| 1 | pub struct Base { |
| 2 | mut: |
| 3 | x int |
| 4 | y int |
| 5 | } |
| 6 | |
| 7 | pub fn (mut b Base) init() {} |
| 8 | |
| 9 | pub fn (b Base) get_pos() (int, int) { |
| 10 | return b.x, b.y |
| 11 | } |
| 12 | |
| 13 | pub struct Label { |
| 14 | Base |
| 15 | } |
| 16 | |
| 17 | pub interface Widget { |
| 18 | mut: |
| 19 | init() |
| 20 | } |
| 21 | |
| 22 | pub interface Layoutable { |
| 23 | get_pos() (int, int) |
| 24 | } |
| 25 | |
| 26 | pub struct Layout { |
| 27 | Base |
| 28 | mut: |
| 29 | widgets []Widget |
| 30 | } |
| 31 | |
| 32 | pub 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 | |
| 46 | fn 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 | |