2021-03-27 20:29:57 +03:00
|
|
|
pub const (
|
2021-07-19 01:18:39 +03:00
|
|
|
a = b
|
|
|
|
ccc = a + b
|
|
|
|
b = 1
|
|
|
|
d = (e / 2) + 7
|
|
|
|
e = 9
|
2019-10-21 08:57:29 +03:00
|
|
|
)
|
|
|
|
|
2020-12-30 04:15:44 +03:00
|
|
|
pub const x = 10
|
|
|
|
|
2019-10-21 08:57:29 +03:00
|
|
|
fn test_const() {
|
2020-03-29 20:26:55 +03:00
|
|
|
assert d == 11
|
2021-07-19 01:18:39 +03:00
|
|
|
//
|
|
|
|
assert b == 1
|
|
|
|
assert a == 1
|
|
|
|
assert ccc == a + b
|
|
|
|
assert e == 9
|
|
|
|
assert d == (e / 2) + 7
|
2019-10-24 12:19:05 +03:00
|
|
|
}
|
2020-12-19 11:43:50 +03:00
|
|
|
|
2023-01-09 09:36:45 +03:00
|
|
|
// const option test
|
2020-12-19 11:43:50 +03:00
|
|
|
struct Foo {
|
|
|
|
name string = 'foo'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo_decode(name string) ?Foo {
|
|
|
|
if name == 'baz' {
|
|
|
|
return error('baz is not allowed')
|
|
|
|
}
|
|
|
|
return Foo{name}
|
|
|
|
}
|
|
|
|
|
2021-03-27 20:29:57 +03:00
|
|
|
pub const (
|
2020-12-19 11:43:50 +03:00
|
|
|
def = foo_decode('baz') or { Foo{} }
|
2022-05-13 06:56:21 +03:00
|
|
|
bar = foo_decode('bar')?
|
2020-12-19 11:43:50 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
fn test_opt_const() {
|
|
|
|
assert def.name == 'foo'
|
|
|
|
assert bar.name == 'bar'
|
|
|
|
}
|
2021-05-09 03:25:44 +03:00
|
|
|
|
|
|
|
// const with expressions that compile to multiple C statements
|
|
|
|
pub const (
|
|
|
|
abc = [1, 2, 3].map(it * it)
|
|
|
|
ghi = [1, 2, 3, 4, 5].filter(it % 2 == 0)
|
|
|
|
jkl = [`a`, `b`, `c`].contains(`d`)
|
|
|
|
)
|
|
|
|
|
|
|
|
fn test_multistmt_const() {
|
|
|
|
assert abc[2] == 9
|
|
|
|
assert ghi.len == 2
|
|
|
|
assert jkl == false
|
|
|
|
}
|