Structs and Enums¶
Structs: form¶
Access with the dot operator:
Enums: state¶
Variants are implicitly 0, 1, 2, ...
Unions: fusion¶
All fields share the same memory:
Bitfields¶
Struct fields can specify a bit width for packed storage:
Use : N after the type to allocate N bits. Useful for hardware registers and compact data.
Enum Payloads¶
Enums can carry data in variants:
Use choose to pattern match and extract payloads.
Struct Methods: impl¶
Add methods to structs with impl blocks:
form Point {
x: f64,
y: f64
}
impl Point {
craft magnitude(self: Point) -> f64 {
send sqrt(self.x * self.x + self.y * self.y);
}
}
craft main() -> void {
make p: Point = ...;
make m: f64 = p.magnitude();
send;
}