6.6. Polymorphic Classes

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