v2 / vlib / v / tests / structs / struct_embed_is_interface_test.v
53 lines · 45 sloc · 656 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1pub struct Base {
2mut:
3 x int
4 y int
5}
6
7pub fn (mut b Base) init() {}
8
9pub fn (b Base) get_pos() (int, int) {
10 return b.x, b.y
11}
12
13pub struct Label {
14 Base
15}
16
17pub interface Widget {
18mut:
19 init()
20}
21
22pub interface Layoutable {
23 get_pos() (int, int)
24}
25
26pub struct Layout {
27 Base
28mut:
29 widgets []Widget
30}
31
32pub fn (mut l Layout) layout() {
33 for wd in l.widgets {
34 if wd is Layoutable {
35 lw := wd as Layoutable
36 dump(lw.get_pos())
37 x, y := lw.get_pos()
38 assert x == 10
39 assert y == 20
40 } else {
41 println('wd is NOT Layoutable')
42 }
43 }
44}
45
46fn test_struct_embed_is_interface() {
47 mut l := Layout{}
48 l.widgets << Label{
49 x: 10
50 y: 20
51 }
52 l.layout()
53}
54