2.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/tutorial/tut-1.13-0.flx[1 /1 ]
     1: #line 721 "./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/tutorial/tut-1.13-0.flx[1]
Start data section to tut/tutorial/tut-1.13-0.expect[1 /1 ]
     1: 1
     2: 2
End data section to tut/tutorial/tut-1.13-0.expect[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/tutorial/tut-1.13-1.flx[1 /1 ]
     1: #line 746 "./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;
End felix section to tut/tutorial/tut-1.13-1.flx[1]
Start data section to tut/tutorial/tut-1.13-1.expect[1 /1 ]
     1: 1
     2: 2
End data section to tut/tutorial/tut-1.13-1.expect[1]