Structs
Structs are similar to tuples with named field values of arbitrary pre-declared types.
Fig 1:
var myStruct: struct {x: int, y: int} = {x = 1, y = 2};
print(myStruct);
Output:
{x = 1, y = 2}
Field Access
Struct field values can be directly accessed using "dot-notation" as below:
Fig 2:
var myStruct: struct {x: int, y: int} = {x = 1, y = 2};
print(myStruct.x);
print(myStruct.y);
Output:
1
2
Mutable Structs
Just like any other builtin collection type, a Claro struct may be declared mutable using the mut
keyword when
declaring a variable or initializing the value. You may then update element values at will as long as the initial type
declaration for each element is honored.
Fig 3:
var myStruct = mut {name = "Jason", age = 29}; # <-- Omitting optional type annotation.
print(myStruct);
myStruct.name = "Claro"; # <-- Mutation happens here.
myStruct.age = 3; # <-- Mutation happens here.
print(myStruct);
Output:
mut {name = Jason, age = 29}
mut {name = Claro, age = 3}