use fmt;
type coords = struct { x: int, y: int };
export fn main() void = {
let player1 = struct {
x: int = 10,
y: int = 20,
};
let player2 = coords {
x = 10,
y = 20,
};
let player3: (int, int) = (42, 24);
fmt::printfln("Player 1: ({}, {})", player1.x, player1.y)!;
fmt::printfln("Player 2: ({}, {})", player2.x, player2.y)!;
fmt::printfln("Player 3: ({}, {})", player3.0, player3.1)!;
};
struct
Structs are one of the composite types supported in hare. These define a
structured value which is made up of other values in a certain order. In this
example, we show two ways of using structs: first manually, and then by using a
user-defined type, for player1 and player2 respectively. We can access the
fields of the struct using the .
operator.
tuple
In the above example player3
is defined with a tuple
type, which is very
similar to a struct, but does not name it's fields. They are accessed by their
original position, starting from zero, instead of their names.