8.1. Polymorphic Classes

Start felix section to tut/tutorial/tut-7.01-0.flx[1 /1 ]
     1: #line 5810 "./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: 
    37: var ob <- new Y[int](99);
    38: ob.x = 2;
    39: ob.y = 3L;
    40: 
    41: print ob.c; endl;
    42: print ob.x; endl;
    43: print ob.y; endl;
    44: print$ ob.fetchc(); endl;
    45: 
    46: proc iprint(x:int) { print x; }
    47: 
    48: ob.setx 22;
    49: ob.yprint(iprint of (int));
    50: 
    51: ob.setxy (12,33L);
    52: ob.yprint(iprint of (int));
    53: 
    54: print$ ob.f (1,add of (int*int)); endl;
    55: 
    56: proc ncl() {
    57:   var x = 42;
    58:   class J {
    59:     var j:int;
    60:     ctor (a:int) { j = a; }
    61:     fun h(q:int):int => q + j + x;
    62:   };
    63:   var b <- new J(100);
    64:   print$ b.h 1000; endl;
    65: };
    66: 
    67: ncl;
    68: 
    69: class A[t] {
    70:   fun f(a:t):t=>a;
    71:   ctor (){}
    72: };
    73: 
    74: module V[t] {
    75:   fun f(a:t):t=>a;
    76: };
    77: 
    78: fun X[t]() = {
    79:   fun f(a:t):t=>a;
    80: }
    81: var aint <- new A[int]();
End felix section to tut/tutorial/tut-7.01-0.flx[1]
Start data section to tut/tutorial/tut-7.01-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: 1142
End data section to tut/tutorial/tut-7.01-0.expect[1]