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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
///|
/// A greeting to print, and how many times to print it.
struct Greeting {
name : String
times : Int
} derive(Eq)
///|
/// Show is written out by hand rather than derived: `derive(Show)` is
/// deprecated, and this is the form the toolchain points at instead.
impl Show for Greeting with fn output(self, logger) {
logger.write_string("\{self.name} × \{self.times}")
}
///|
/// The tone a greeting is delivered in.
enum Tone {
Plain
Loud
Question
} derive(Debug)
///|
/// punctuate returns the mark a tone ends on.
fn punctuate(tone : Tone) -> String {
match tone {
Plain => "."
Loud => "!"
Question => "?"
}
}
///|
/// greet prints one greeting, once per `times`.
///
/// `tone` is a labelled argument with a default, so a call site may leave it
/// out — and when it does not, it reads as `greet(g, tone=Loud)`.
fn greet(g : Greeting, tone? : Tone = Plain) -> Unit {
for i in 0..<g.times {
println("Hello, \{g.name}\{punctuate(tone)} (\{i + 1} of \{g.times})")
}
}
///|
fn main {
let g = { name: "MoonBit", times: 2, }
for tone in [Plain, Loud, Question] {
greet(g, tone~)
}
println("")
println("the greeting itself: \{g}")
}
|