enum Shape { Circle(r: float) Rect(w, h: float) Point func area(): float { return match self { Circle(r) => 3.14159 * r * r Rect(w, h) => w * h Point => 0 } } func name(): string { return match self { Circle(_) => "circle" Rect(_, _) => "rectangle" Point => "point" } } } enum Color { Red Green Blue } func describe(c: Color): string { return match c { Color.Red => "warm" Color.Green => "natural" Color.Blue => "cool" } } func classify(n: int): string { return match n { 0 => "zero" 1 => "one" _ => "many" } } func main() { var shapes = [Shape.Circle(1.5), Shape.Rect(2, 3), Shape.Point] for s in shapes { print(s.name(), s.area()) } print(shapes) var c = Color.Green print(c, describe(c), c == Color.Green, c != Color.Blue) for i in 0..4 { print(i, classify(i)) } // match as a statement can use blocks and loop control var total = 0.0 for s in shapes { match s { Circle(r) => { total += r } Rect(w, _) => { if w > 100 { break } total += w } _ => continue } print("so far", total) } // bool and string matches var flag = true print(match flag { true => "yes" false => "no" }) var cmd = "stop" match cmd { "go" => print("going") "stop" => print("stopping") other => print("unknown:", other) } // the zero value of an enum is its first case var z: Shape print(z, Color.Red == (Color.Red)) }