-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenum_examples.gnt
More file actions
93 lines (72 loc) · 1.68 KB
/
enum_examples.gnt
File metadata and controls
93 lines (72 loc) · 1.68 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Enum Examples
enum Status {
Pending
Active
Completed
Failed(message)
}
enum Color {
Red
Green
Blue
RGB(r: number, g: number, b: number)
}
fn demoBasicEnums() {
println("=== Basic Enums ===")
let s1 = Status.Pending
let s2 = Status.Failed("connection timeout")
println("s1: {s1}")
println("s2: {s2}")
println()
}
fn demoIsMethod() {
println("=== .is() Method ===")
let status = Status.Active
if status.is(Status.Active) {
println("Status is active!")
}
if status.is(Status.Pending) {
println("This won't print")
} else {
println("Status is not pending")
}
println()
}
fn demoMatch() {
println("=== Match Expression ===")
let status = Status.Failed("disk full")
let message = match status {
Status.Pending => "Waiting to start..."
Status.Active => "Currently running"
Status.Completed => "All done!"
Status.Failed(msg) => "Error: {msg}"
}
println("Result: {message}")
println()
}
fn demoColorMatch() {
println("=== Color Match ===")
let color = Color.RGB(255, 128, 0)
let desc = match color {
Color.Red => "Pure red"
Color.Green => "Pure green"
Color.Blue => "Pure blue"
Color.RGB(r, g, b) => "RGB({r}, {g}, {b})"
}
println("Color: {desc}")
println()
}
fn demoDataMethod() {
println("=== .data() Method ===")
let result = Status.Failed("not found")
let msg = result.data(0)
println("Error message: {msg}")
println()
}
// Run all demos
demoBasicEnums()
demoIsMethod()
demoMatch()
demoColorMatch()
demoDataMethod()
println("=== Enum examples complete! ===")