| 📦 Turbo MoonBit cc1f595 k33g yesterday | 1 | ///| |
| 2 | /// Measures a handful of shapes and prints what it found. |
| 3 | /// |
| 4 | /// Run it from the project root with `moon run cmd/main`, or from the |
| 5 | /// editor's MoonBit menu with **Run** and `cmd/main`. |
| 6 | fn main { |
| 7 | let circles = [ |
| 8 | @shapes.Circle::{ radius: 1.0, }, |
| 9 | @shapes.Circle::{ radius: 2.5, }, |
| 10 | @shapes.Circle::{ radius: 0.5, }, |
| 11 | ] |
| 12 | let rects = [ |
| 13 | @shapes.Rect::{ width: 3.0, height: 4.0, }, |
| 14 | @shapes.Rect::{ width: 1.5, height: 1.5, }, |
| 15 | ] |
| 16 | report("circles", circles) |
| 17 | report("rectangles", rects) |
| 18 | report("nothing at all", ([] : Array[@shapes.Circle])) |
| 19 | } |
| 20 | |
| 21 | ///| |
| 22 | /// report prints the total and the largest of a list, and says so plainly when |
| 23 | /// the list is empty rather than letting the error escape. |
| 24 | fn[T : @shapes.Area + Show] report(what : String, shapes : Array[T]) -> Unit { |
| 25 | println("── \{what} ──") |
| 26 | try { |
| 27 | let sum = @shapes.total(shapes) |
| 28 | println(" total area: \{sum}") |
| 29 | } catch { |
| 30 | @shapes.NoShapes => println(" no shapes were given") |
| 31 | } |
| 32 | match @shapes.largest(shapes) { |
| 33 | Some(shape) => println(" largest: \{shape}") |
| 34 | None => println(" largest: none") |
| 35 | } |
| 36 | println("") |
| 37 | } |