import std.math.*
// 枚举类型
enum Shape {
| Circle(Float64)
| Rectangle(Float64, Float64)
| Square(Float64)
// Pattern Matching
public static func area(shape: Shape): Unit {
let s = match (shape) {
case Circle(radius) => radius * radius * Float64.PI
case Rectangle(width, height) => width * height
case Square(side) => side * side
}
"Area: ${s}" |> println
}
public static func perimeter(shape: Shape): Unit {
let c = match (shape) {
case Circle(radius) => radius * 2.0 * Float64.PI
case Rectangle(width, height) => (width + height) * 2.0
case Square(side) => side * 4.0
}
"Perimeter: ${c}" |> println
}
}
main() {
let circle = Circle(5.0)
circle |> Shape.area
circle |> Shape.perimeter
let rectangle = Rectangle(4.0, 6.0)
rectangle |> Shape.area
rectangle |> Shape.perimeter
let square = Square(7.0)
square |> Shape.area
square |> Shape.perimeter
}