6.7. Polymorphic Methods

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