Tin

Examples · learn/06-enums · 117 of 339

payload

Each variant can carry its own data.

examples/learn/06-enums/payload.tintin run examples/learn/06-enums/payload.tin
// Each variant can carry its own data.
type Shape =
  | Circle { radius: float }
  | Rect { width: float, height: float }
  | Point

fn area(s: Shape): float => match s {
  Circle { radius } => PI * radius * radius
  Rect { width, height } => width * height
  Point => 0.0
}

fn main() {
  print(area(Circle { radius: 1.0 }))
  print(area(Rect { width: 2.0, height: 3.0 }))
  print(area(Point))
}