2019-07-02 00:00:27 +02:00
|
|
|
struct A{
|
|
|
|
mut:
|
|
|
|
val int
|
|
|
|
nums []int
|
|
|
|
}
|
|
|
|
|
|
|
|
struct B{
|
|
|
|
mut:
|
|
|
|
a A
|
|
|
|
}
|
2019-07-03 23:53:48 +02:00
|
|
|
|
2019-07-02 00:00:27 +02:00
|
|
|
struct C {
|
|
|
|
mut:
|
|
|
|
b B
|
|
|
|
nums []int
|
2019-07-13 12:15:16 +02:00
|
|
|
aarr []A
|
2019-07-02 00:00:27 +02:00
|
|
|
num int
|
|
|
|
}
|
2019-07-03 23:53:48 +02:00
|
|
|
|
|
|
|
struct User {
|
|
|
|
name string
|
|
|
|
age int
|
|
|
|
}
|
2019-07-24 02:22:34 +02:00
|
|
|
|
|
|
|
struct Foo {
|
|
|
|
@type string
|
|
|
|
}
|
2019-07-02 00:00:27 +02:00
|
|
|
|
|
|
|
fn test_struct_levels() {
|
|
|
|
mut c := C{}
|
|
|
|
assert c.nums.len == 0
|
|
|
|
c.nums << 3
|
|
|
|
assert c.nums.len == 1
|
|
|
|
assert c.nums[0] == 3
|
|
|
|
c.nums[0] = 4
|
|
|
|
assert c.nums[0] == 4
|
|
|
|
c.b.a.val = 34
|
|
|
|
assert c.b.a.val == 34
|
|
|
|
c.b.a.nums = [0;0]
|
|
|
|
c.b.a.nums << 0
|
|
|
|
c.b.a.nums << 2
|
|
|
|
assert c.b.a.nums.len == 2
|
|
|
|
assert c.b.a.nums[0] == 0
|
|
|
|
assert c.b.a.nums[1] == 2
|
|
|
|
c.b.a.nums [0] = 7
|
|
|
|
assert c.b.a.nums[0] == 7
|
2019-07-13 12:15:16 +02:00
|
|
|
c.aarr << A{val:8}
|
|
|
|
assert c.aarr.len == 1
|
|
|
|
assert c.aarr[0].val == 8
|
2019-07-02 00:00:27 +02:00
|
|
|
c.num = 20
|
|
|
|
assert c.num == 20
|
2019-07-13 12:15:16 +02:00
|
|
|
c.aarr[0].val = 10
|
|
|
|
assert c.aarr[0].val == 10
|
2019-07-02 00:00:27 +02:00
|
|
|
}
|
2019-07-03 23:53:48 +02:00
|
|
|
|
|
|
|
fn test_struct_str() {
|
|
|
|
u := User{'Bob', 30}
|
|
|
|
println(u) // make sure the struct is printable
|
|
|
|
// assert u.str() == '{name:"Bob", age:30}' // TODO
|
|
|
|
}
|
2019-07-24 02:22:34 +02:00
|
|
|
|
|
|
|
fn test_at() {
|
|
|
|
foo := Foo{ @type: 'test' }
|
|
|
|
println(foo.@type)
|
|
|
|
}
|