| 1 |
var global := 0 |
| 2 |
|
| 3 |
func test(): |
| 4 |
var a = 0 |
| 5 |
var b = 1 |
| 6 |
|
| 7 |
match a: |
| 8 |
0 when b == 0: |
| 9 |
print("does not run" if true else "") |
| 10 |
0 when b == 1: |
| 11 |
print("guards work") |
| 12 |
_: |
| 13 |
print("does not run") |
| 14 |
|
| 15 |
match a: |
| 16 |
var a_bind when b == 0: |
| 17 |
prints("a is", a_bind, "and b is 0") |
| 18 |
var a_bind when b == 1: |
| 19 |
prints("a is", a_bind, "and b is 1") |
| 20 |
_: |
| 21 |
print("does not run") |
| 22 |
|
| 23 |
match a: |
| 24 |
var a_bind when a_bind < 0: |
| 25 |
print("a is less than zero") |
| 26 |
var a_bind when a_bind == 0: |
| 27 |
print("a is equal to zero") |
| 28 |
_: |
| 29 |
print("a is more than zero") |
| 30 |
|
| 31 |
match [1, 2, 3]: |
| 32 |
[1, 2, var element] when element == 0: |
| 33 |
print("does not run") |
| 34 |
[1, 2, var element] when element == 3: |
| 35 |
print("3rd element is 3") |
| 36 |
|
| 37 |
match a: |
| 38 |
_ when b == 0: |
| 39 |
print("does not run") |
| 40 |
_ when b == 1: |
| 41 |
print("works with wildcard too.") |
| 42 |
_: |
| 43 |
print("does not run") |
| 44 |
|
| 45 |
match a: |
| 46 |
0, 1 when b == 0: |
| 47 |
print("does not run") |
| 48 |
0, 1 when b == 1: |
| 49 |
print("guard with multiple patterns") |
| 50 |
_: |
| 51 |
print("does not run") |
| 52 |
|
| 53 |
match a: |
| 54 |
0 when b == 0: |
| 55 |
print("does not run") |
| 56 |
0: |
| 57 |
print("regular pattern after guard mismatch") |
| 58 |
|
| 59 |
match a: |
| 60 |
1 when side_effect(): |
| 61 |
print("should not run the side effect call") |
| 62 |
0 when side_effect(): |
| 63 |
print("will run the side effect call, but not this") |
| 64 |
_: |
| 65 |
Utils.check(global == 1) |
| 66 |
print("side effect only ran once") |
| 67 |
|
| 68 |
func side_effect(): |
| 69 |
print("side effect") |
| 70 |
global += 1 |
| 71 |
return false |