8.2. Polymorphic Methods

Start felix section to tut/tutorial/tut-7.02-0.flx[1 /1 ]
     1: #line 5904 "./lpsrc/flx_tutorial.pak"
     2: //Check polymorphism
     3: //Check classes
     4: #import <flx.flxh>
     5: open Long;
     6: 
     7: class Y[t] {
     8:   // variables
     9:   val c : int;
    10:   var x : t;
    11:   var y : long;
    12: 
    13:   // accessor methods
    14:   fun fetchc():int =>c;
    15: 
    16:   // mutators
    17:   proc setx(a:t) { x = a; }
    18:   proc setxy( a:t, b:long) { x = a; y = b; }
    19: 
    20:   // accessor procedures
    21:   proc yprint(xprint:t->0) {
    22:     print "This is a Y object, with x = ";
    23:     xprint x; print ", y = "; print y;
    24:     print ", and c = "; print c; endl;
    25:   }
    26: 
    27:   // test methods calling methods
    28: 
    29:   fun f(a:int,add:int * t->int):int => g$ a,add;
    30:   fun g(a:int,add:int * t->int):int => a + x;
    31: 
    32:   // constructors
    33:   ctor () {}
    34:   ctor (a:int): c(20000) { x = a; }
    35: 
    36:   // polymorphic method
    37:   fun p[u](a:u, add:t * u ->int):int => x + a;
    38: };
    39: 
    40: var ob <- new Y[int](99);
    41: ob.x = 2;
    42: ob.y = 3L;
    43: 
    44: print ob.c; endl;
    45: print ob.x; endl;
    46: print ob.y; endl;
    47: print$ ob.fetchc(); endl;
    48: 
    49: proc iprint(x:int) { print x; }
    50: 
    51: ob.setx 22;
    52: ob.yprint(iprint of (int));
    53: 
    54: ob.setxy (12,33L);
    55: ob.yprint(iprint of (int));
    56: 
    57: print$ ob.f (1,add of (int*int)); endl;
    58: print$ ob.p[int] (1,add of (int*int)); endl;
End felix section to tut/tutorial/tut-7.02-0.flx[1]
Start data section to tut/tutorial/tut-7.02-0.expect[1 /1 ]
     1: 20000
     2: 2
     3: 3
     4: 20000
     5: This is a Y object, with x = 22, y = 3, and c = 20000
     6: This is a Y object, with x = 12, y = 33, and c = 20000
     7: 13
     8: 13
End data section to tut/tutorial/tut-7.02-0.expect[1]