Slide 12 of 37
Declared types
A struct
type User {
id: Int
name: Str
}
proc main(): void {
u := User(1, "ada")
echo u.name
}
A tagged union
type Shape {
Circle { radius: Int }
Rectangle { width: Int, height: Int }
Point
}
proc main(): void {
p := Shape.Circle(5)
echo p
}
A type with no variants is a struct; with variants, a tagged union. A field declared outside any variant is added to every variant. A value is built by calling the type or the variant: User(1, "ada"), Shape.Circle(5). There is no subtyping and no interface — a variant is a way of building and matching a union, not a type of its own.