| 1 | const version = 123 |
| 2 | const disable_opt_features = true |
| 3 | |
| 4 | // Note: the `unknown_fn()` calls are here on purpose, to make sure that anything |
| 5 | // that doesn't match a compile-time condition is not even parsed. |
| 6 | fn test_ct_expressions() { |
| 7 | mut result := '' |
| 8 | foo := version |
| 9 | bar := foo |
| 10 | $if bar == 123 { |
| 11 | result += 'a' |
| 12 | assert true |
| 13 | } $else { |
| 14 | unknown_fn() |
| 15 | } |
| 16 | |
| 17 | $if bar != 123 { |
| 18 | unknown_fn() |
| 19 | } $else $if bar != 124 { |
| 20 | result += 'b' |
| 21 | assert true |
| 22 | } $else { |
| 23 | unknown_fn() |
| 24 | } |
| 25 | |
| 26 | $if !disable_opt_features { |
| 27 | unknown_fn() |
| 28 | } $else { |
| 29 | result += 'c' |
| 30 | assert true |
| 31 | } |
| 32 | assert result == 'abc' |
| 33 | } |
| 34 | |
| 35 | fn generic_t_is[O]() O { |
| 36 | $if O is string { |
| 37 | return "It's a string!" |
| 38 | } $else { |
| 39 | return O{} |
| 40 | } |
| 41 | return O{} |
| 42 | } |
| 43 | |
| 44 | struct GenericTIsTest {} |
| 45 | |
| 46 | fn test_generic_t_is() { |
| 47 | assert generic_t_is[string]() == "It's a string!" |
| 48 | assert generic_t_is[GenericTIsTest]() == GenericTIsTest{} |
| 49 | } |
| 50 | |
| 51 | fn generic_t_is2[T]() ?T { |
| 52 | $if T is string { |
| 53 | return "It's a string!" |
| 54 | } $else { |
| 55 | return T{} |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | fn test_generic_t_is2() { |
| 60 | res := generic_t_is2[string]() or { |
| 61 | assert false |
| 62 | '' |
| 63 | } |
| 64 | res2 := generic_t_is2[GenericTIsTest]() or { |
| 65 | assert false |
| 66 | GenericTIsTest{} |
| 67 | } |
| 68 | assert res == "It's a string!" |
| 69 | assert res2 == GenericTIsTest{} |
| 70 | } |
| 71 | |
| 72 | fn generic_t_is3[T](raw_data string) ?T { |
| 73 | $if T is string { |
| 74 | return '' |
| 75 | } |
| 76 | return T{} |
| 77 | } |
| 78 | |
| 79 | fn test_generic_t_is3() { |
| 80 | res := generic_t_is3[GenericTIsTest]('') or { |
| 81 | assert false |
| 82 | GenericTIsTest{} |
| 83 | } |
| 84 | assert res == GenericTIsTest{} |
| 85 | } |
| 86 | |
| 87 | fn generic_t_is_with_else[T](raw_data string) ?T { |
| 88 | $if T is string { |
| 89 | return raw_data |
| 90 | } $else { |
| 91 | return T{} |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | fn test_generic_t_is_with_else() { |
| 96 | res := generic_t_is_with_else[GenericTIsTest]('') or { |
| 97 | assert false |
| 98 | GenericTIsTest{} |
| 99 | } |
| 100 | assert res == GenericTIsTest{} |
| 101 | str := generic_t_is_with_else[string]('test') or { |
| 102 | assert false |
| 103 | '' |
| 104 | } |
| 105 | assert str == 'test' |
| 106 | } |
| 107 | |
| 108 | fn generic_t_is_with_else_if[T]() []string { |
| 109 | mut fields := []string{} |
| 110 | $for field in T.fields { |
| 111 | $if field.typ is string { |
| 112 | fields << field.name |
| 113 | } $else $if field.typ is int { |
| 114 | fields << field.name |
| 115 | } |
| 116 | } |
| 117 | return fields |
| 118 | } |
| 119 | |
| 120 | struct User { |
| 121 | name string |
| 122 | age int |
| 123 | } |
| 124 | |
| 125 | fn test_generic_t_is_with_else_if() { |
| 126 | x := generic_t_is_with_else_if[User]() |
| 127 | assert x == ['name', 'age'] |
| 128 | } |
| 129 | |