| 1 | pub const a = b |
| 2 | pub const ccc = a + b |
| 3 | pub const b = 1 |
| 4 | pub const d = (e / 2) + 7 |
| 5 | pub const e = 9 |
| 6 | |
| 7 | pub const x = 10 |
| 8 | |
| 9 | fn test_const() { |
| 10 | assert d == 11 |
| 11 | |
| 12 | assert b == 1 |
| 13 | assert a == 1 |
| 14 | assert ccc == a + b |
| 15 | assert e == 9 |
| 16 | assert d == (e / 2) + 7 |
| 17 | } |
| 18 | |
| 19 | // const option test |
| 20 | struct Foo { |
| 21 | name string = 'foo' |
| 22 | } |
| 23 | |
| 24 | fn foo_decode(name string) !Foo { |
| 25 | if name == 'baz' { |
| 26 | return error('baz is not allowed') |
| 27 | } |
| 28 | return Foo{name} |
| 29 | } |
| 30 | |
| 31 | pub const def = foo_decode('baz') or { Foo{} } |
| 32 | pub const bar = foo_decode('bar')! |
| 33 | |
| 34 | fn test_opt_const() { |
| 35 | assert def.name == 'foo' |
| 36 | assert bar.name == 'bar' |
| 37 | } |
| 38 | |
| 39 | // const with expressions that compile to multiple C statements |
| 40 | pub const abc = [1, 2, 3].map(it * it) |
| 41 | pub const ghi = [1, 2, 3, 4, 5].filter(it % 2 == 0) |
| 42 | pub const jkl = [`a`, `b`, `c`].contains(`d`) |
| 43 | |
| 44 | fn test_multistmt_const() { |
| 45 | assert abc[2] == 9 |
| 46 | assert ghi.len == 2 |
| 47 | assert jkl == false |
| 48 | } |
| 49 | |