1.13. Structs

Felix supports C like structs. A struct, like a tuple, is a categorical product type. Unlike a tuple, a struct is named, its members are named, and its members are mutable.

Struct members can be used with C style dot notation. Here is an example:

Start felix section to tut/examples/tut_beg115.flx[1 /1 ]
     1: #line 559 "./lpsrc/flx_tutorial.pak"
     2: #import <flx.flxh>
     3: 
     4: struct XY {
     5:   x : int;
     6:   y : int;
     7: }
     8: 
     9: var xy : XY;
    10: xy.x = 1;
    11: xy.y = 2;
    12: print xy.x; endl;
    13: print xy.y;  endl;
End felix section to tut/examples/tut_beg115.flx[1]
The name of a struct is also the name of a function which constructs an object of the struct type from a tuple consisting of values to initialise the members in sequence. For example:
Start felix section to tut/examples/tut_beg116.flx[1 /1 ]
     1: #line 577 "./lpsrc/flx_tutorial.pak"
     2: #import <flx.flxh>
     3: 
     4: struct XY = {
     5:   x : int;
     6:   y : int;
     7: }
     8: 
     9: val xy = XY(1,2);
    10: print xy.x; endl;
    11: print xy.y; endl;
    12: 
End felix section to tut/examples/tut_beg116.flx[1]