Tin

Examples · learn/06-enums · 116 of 339

guards

examples/learn/06-enums/guards.tintin run examples/learn/06-enums/guards.tin
type Reading = | Temp(float) | Offline

fn describe(r: Reading): string => match r {
  Temp(c) if c > 30.0 => "hot: {c}"     // a guard: this arm matches only when it holds
  Temp(c) if c < 5.0 => "cold: {c}"
  Temp(c) => "fine: {c}"
  Offline => "no data"
}

fn main() {
  print(describe(Temp(34.5)))
  print(describe(Temp(1.0)))
  print(describe(Temp(20.0)))
  print(describe(Offline))
}