| 1 | struct Vec2d { |
| 2 | x int |
| 3 | y int |
| 4 | } |
| 5 | |
| 6 | struct Vec3d { |
| 7 | x int |
| 8 | y int |
| 9 | z int |
| 10 | } |
| 11 | |
| 12 | type Vec = Vec2d | Vec3d |
| 13 | type SumType = int | string | Vec2d | []Vec2d |
| 14 | |
| 15 | fn match_vec(v Vec) { |
| 16 | match v { |
| 17 | Vec2d { |
| 18 | println('Vec2d(${v.x},${v.y})') |
| 19 | } |
| 20 | Vec3d { |
| 21 | println('Vec2d(${v.x},${v.y},${v.z})') |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | fn match_classic_num() { |
| 27 | a := 42 |
| 28 | match a { |
| 29 | 0 { |
| 30 | assert false |
| 31 | (false) |
| 32 | } |
| 33 | 1 { |
| 34 | assert false |
| 35 | (false) |
| 36 | } |
| 37 | 42 { |
| 38 | println('life') |
| 39 | } |
| 40 | else { |
| 41 | assert false |
| 42 | (false) |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | fn match_classic_string() { |
| 48 | os := 'JS' |
| 49 | print('V is running on ') |
| 50 | match os { |
| 51 | 'darwin' { println('macOS.') } |
| 52 | 'linux' { println('Linux.') } |
| 53 | else { println(os) } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | fn match_bool_cond() { |
| 58 | volume := 'c:' |
| 59 | rooted := false |
| 60 | path_separator := '/' |
| 61 | println(match true { |
| 62 | volume.len != 0 { volume } |
| 63 | !rooted { '.' } |
| 64 | else { path_separator } |
| 65 | }) |
| 66 | } |
| 67 | |
| 68 | fn match_sum_type(sum SumType) { |
| 69 | match sum { |
| 70 | int { |
| 71 | println('sum is int') |
| 72 | } |
| 73 | string { |
| 74 | println('sum is string') |
| 75 | } |
| 76 | Vec2d { |
| 77 | println('sum is Vec2d') |
| 78 | } |
| 79 | []Vec2d { |
| 80 | println('sum is []Vec2d') |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | fn main() { |
| 86 | match_vec(Vec2d{42, 43}) |
| 87 | match_vec(Vec3d{46, 74, 21}) |
| 88 | match_classic_num() |
| 89 | match_classic_string() |
| 90 | match_bool_cond() |
| 91 | match_sum_type(42) |
| 92 | match_sum_type('everything') |
| 93 | match_sum_type(Vec2d{7, 11}) |
| 94 | match_sum_type([Vec2d{7, 11}, Vec2d{13, 17}]) |
| 95 | } |
| 96 | |