2.19. Objects

Felix provides a way of making objects from functions. An object is just a struct whose members are functions nested in the scope of the object constructor function. Variables in the constructor are hidden.

Note: the type of a member function must not depend on a name or type defined in the object (this constraint is because the desugaring precedes name binding).

Note: object constructors are not members, because their types would be local to the constructor (see above).

Note: The name of the type of an object with constructor 'x' is '_ot_x': the constructor name prefixed by '_ot_' (Ugly).

Note: you can change the methods of an object by assigning a new functional value to the structure component.

Note: the type of the object is not part of a method signature: their type is as written.

Note: only explicitly declared non-private functions (and procedures) are taken as methods. Implicit functions such as blocks are not taken.

Start felix section to tut/tutorial/tut-1.19-0.flx[1 /1 ]
     1: #line 1043 "./lpsrc/flx_tutorial.pak"
     2: #import <flx.flxh>
     3: obj a(x:int) {
     4:   var v = x;
     5:   fun fetch ():int = { return v; }
     6:   proc store (y:int) { v = y; }
     7: }
     8: 
     9: val z = a(1);
    10: print (z.fetch()); endl;
    11: z.store(2);
    12: print (z.fetch()); endl;
End felix section to tut/tutorial/tut-1.19-0.flx[1]
Start data section to tut/tutorial/tut-1.19-0.expect[1 /1 ]
     1: 1
     2: 2
End data section to tut/tutorial/tut-1.19-0.expect[1]