v2 / vlib / v / tests / options / option_selector_interface_ptr_test.v
35 lines · 31 sloc · 589 bytes · 5b5e845f0f9b2c181ba7ab984b674201d6996b9d
Raw
1module main
2
3interface IGameObject {
4mut:
5 name string
6 parent ?&IGameObject
7 children []&IGameObject
8 add_child(mut o IGameObject)
9}
10
11@[heap]
12struct GameObject implements IGameObject {
13mut:
14 name string
15 parent ?&IGameObject
16 children []&IGameObject
17}
18
19fn (mut gameobject GameObject) add_child(mut o IGameObject) {
20 o.parent = gameobject
21 gameobject.children << o
22}
23
24fn test_main() {
25 mut v1 := &GameObject{
26 name: 'v1'
27 }
28 mut v2 := &GameObject{
29 name: 'v2'
30 }
31 v1.add_child(mut v2)
32 if v1.children[0].parent != none {
33 assert '${v1.children[0].parent.name}' == 'v1'
34 }
35}
36