v2 / vlib / v / gen / wasm / tests / match.vv
104 lines · 90 sloc · 1.41 KB · d6eb7a59747f2fa9a524a9207b16fa62ab5793a6
Raw
1enum BasicTestEnum {
2 first_element
3 second_element
4 other_element
5}
6
7fn function_matches(value u8) {
8 match value {
9 42 {
10 println('function_matches: OK')
11 }
12 else {
13 println('function_matches: Error')
14 }
15 }
16}
17
18fn main() {
19 a := 1
20 match a {
21 1 { println('1') }
22 else { println('other') }
23 }
24
25 b := 2
26 match b {
27 1 { println('1') }
28 2 { println('2') }
29 3 { println('3') }
30 4 { println('4') }
31 5 { println('5') }
32 6 { println('6') }
33 7 { println('7') }
34 8 { println('8') }
35 9 { println('9') }
36 else { println('other') }
37 }
38
39 c := 3
40 match c {
41 1, 2 { println('1 or 2') }
42 else { println('other') }
43 }
44
45 d := match a {
46 1 { 10 }
47 else { 30 }
48 }
49 println(d)
50
51 e := match b {
52 1 { 100 }
53 2 { 200 }
54 3, 4 { 340 }
55 else { 300 }
56 }
57 println(e)
58
59 println(match c {
60 1, 2 { i64(1000) }
61 else { i64(2000) }
62 })
63
64 println('Enum matches -------')
65
66 f := BasicTestEnum.first_element
67 println(match f {
68 .first_element { 'First Element' }
69 .second_element { 'Second Element' }
70 else { 'Other Element' }
71 })
72
73 println('Bool matches -------')
74
75 println(match true {
76 true {
77 'OK'
78 }
79 else {
80 'ERROR'
81 }
82 })
83
84 g := false
85 println(match g {
86 false {
87 'OK'
88 }
89 else {
90 'ERROR'
91 }
92 })
93
94 println('Function calling matches -------')
95 function_matches(42)
96
97 println('Strings matches -------')
98 h := 'Hello'
99 println(match h {
100 'NotHello' { 'Not Hello string' }
101 'Hello' { 'Hello string' }
102 else { 'Unknown' }
103 })
104}
105