| 1 | fn test_interface_embedding_call() { |
| 2 | g1 := G1{} |
| 3 | do_the_greet(g1) |
| 4 | } |
| 5 | |
| 6 | struct G1 {} |
| 7 | |
| 8 | fn (g G1) greet() string { |
| 9 | return 'hello from G1' |
| 10 | } |
| 11 | |
| 12 | fn do_the_greet(g ParentGreeter) { |
| 13 | greet := g.greet() |
| 14 | println('Someone says: ${greet}') |
| 15 | assert greet == 'hello from G1' |
| 16 | } |
| 17 | |
| 18 | interface ParentGreeter { |
| 19 | Greeter |
| 20 | } |
| 21 | |
| 22 | interface Greeter { |
| 23 | greet() string |
| 24 | } |
| 25 | |
| 26 | // for issue 16496, test the own methods. |
| 27 | interface Foo { |
| 28 | a_method() |
| 29 | } |
| 30 | |
| 31 | fn (f Foo) foo_method() int { |
| 32 | return 0 |
| 33 | } |
| 34 | |
| 35 | interface Bar { |
| 36 | Foo |
| 37 | } |
| 38 | |
| 39 | fn (b Bar) bar_method() int { |
| 40 | // The test calls the method of the embedded interface in the interface method |
| 41 | return b.foo_method() |
| 42 | } |
| 43 | |
| 44 | struct Derived {} |
| 45 | |
| 46 | fn (d &Derived) a_method() { |
| 47 | } |
| 48 | |
| 49 | fn test_embedding_method_call_cgen() { |
| 50 | bar := Bar(Derived{}) |
| 51 | assert bar.bar_method() == 0 |
| 52 | } |
| 53 | |
| 54 | // for issue 20113, test call the method signatures |
| 55 | interface IFoo[T] { |
| 56 | method(arg T) T |
| 57 | } |
| 58 | |
| 59 | interface IBar[T] { |
| 60 | IFoo |
| 61 | } |
| 62 | |
| 63 | interface IBaz[T] { |
| 64 | IBar |
| 65 | } |
| 66 | |
| 67 | struct DerivedStruct[T] { |
| 68 | } |
| 69 | |
| 70 | fn (d DerivedStruct[T]) method[T](arg T) T { |
| 71 | return arg |
| 72 | } |
| 73 | |
| 74 | fn main() { |
| 75 | a := IBaz[int](DerivedStruct[int]{}) |
| 76 | assert a.method(1) == 1 |
| 77 | } |
| 78 | |