1.18. 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 C++ section to tut/examples/tut120a.flx[1 /1 ]
     1: include "std";
     2: obj a(x:int) {
     3:   var v = x;
     4:   fun fetch ():int = { return v; }
     5:   proc store (y:int) { v = y; }
     6: }
     7: 
     8: val z = a(1);
     9: print (z.fetch()); endl;
    10: z.store(2);
    11: print (z.fetch()); endl;
    12: 
End C++ section to tut/examples/tut120a.flx[1]