module demo.Shapes # A struct is a named record. Its fields are read with a colon: p: x(). struct Point = { x, y } # A union is one type with several forms. Each variant may carry fields, and # every variant gets an is() predicate for free. union Shape = { Circle = { radius } Rect = { width, height } } # An augmentation adds functions to a type after the fact. `this` is the value # the function is called on. augment Point { function describe = |this| -> "(" + this: x() + ", " + this: y() + ")" } augment Shape { function area = |this| { return match { when this: isCircle() then 3.14159 * this: radius() * this: radius() when this: isRect() then this: width() * this: height() otherwise 0 } } } function main = |args| { let origin = Point(0, 0) println("origin is " + origin: describe()) let shapes = list[Shape_Circle(1.0), Shape_Rect(2.0, 3.0)] foreach shape in shapes { println(shape + " has area " + shape: area()) } # A closure is a function value: |parameters| -> expression. let scaled = list[shape: area() * 2 foreach shape in shapes] println("doubled: " + scaled) }