Use the `structure` keyword to define data shapes.
structure Point {
x;
y;
};
p = Point();
p.x = 10;
p.y = 20;
You can define an initialization method named constructor.
structure Point {
x; y;
function constructor(x, y) {
this.x = x;
this.y = y;
}
}
p = Point(10, 20);
Structures can inherit fields and methods using extends.
structure Shape { area; }
# Calculated Parent Fields
structure Square extends Shape(width * width) {
width;
}
s = Square(5);
print(s.area); # 25
Use the static keyword for fields and methods belonging to the structure type rather than the instance.
structure MathUtils {
static PI;
static function circleArea(r) {
return MathUtils.PI * r * r;
}
}
MathUtils.PI = 3.14159;
Methods can be added to structures or instances at runtime using addMethod.
structure User { name; }
function sayHello() { return "Hello, " + this.name; }
# Add to structure (affects all new instances)
User.addMethod("hello", sayHello);