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