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 C++ section to tut/examples/tut115.flx[1
/1
]
1: include "std";
2:
3: struct XY {
4: x : int;
5: y : int;
6: }
7:
8: var xy : XY;
9: xy.x = 1;
10: xy.y = 2;
11: print xy.x; endl;
12: print xy.y; endl;
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 C++ section to tut/examples/tut116.flx[1
/1
]
1: include "std";
2:
3: struct XY = {
4: x : int;
5: y : int;
6: }
7:
8: val xy = XY(1,2);
9: print xy.x; endl;
10: print xy.y; endl;
11: