| 1 | interface Foo[T] { |
| 2 | val T |
| 3 | foo() T |
| 4 | } |
| 5 | |
| 6 | struct Bar[T] implements Foo[T] { |
| 7 | val T |
| 8 | } |
| 9 | |
| 10 | fn (b Bar[T]) foo() T { |
| 11 | return b.val |
| 12 | } |
| 13 | |
| 14 | fn test_main() { |
| 15 | b := Bar{ |
| 16 | val: 0 |
| 17 | } |
| 18 | assert b.foo() == 0 |
| 19 | } |
| 20 | |
| 21 | interface Any {} |
| 22 | |
| 23 | interface AInterface { |
| 24 | a int |
| 25 | } |
| 26 | |
| 27 | struct StructA { |
| 28 | mut: |
| 29 | a int |
| 30 | } |
| 31 | |
| 32 | struct Container { |
| 33 | mut: |
| 34 | any Any |
| 35 | } |
| 36 | |
| 37 | fn (mut c Container) set[T](obj Any) { |
| 38 | c.any = obj |
| 39 | } |
| 40 | |
| 41 | fn (c &Container) get[T]() !&T { |
| 42 | if c.any is T { |
| 43 | return c.any |
| 44 | } |
| 45 | return error('ops') |
| 46 | } |
| 47 | |
| 48 | fn test_generic_interface_value_can_be_returned_as_interface_ref() { |
| 49 | a := &StructA{ |
| 50 | a: 100 |
| 51 | } |
| 52 | b := &StructA{ |
| 53 | a: 200 |
| 54 | } |
| 55 | mut c := &Container{ |
| 56 | any: a |
| 57 | } |
| 58 | c.set[StructA](b) |
| 59 | c.set[AInterface](b) |
| 60 | d := c.get[StructA]() or { panic(err) } |
| 61 | e := c.get[AInterface]() or { panic(err) } |
| 62 | assert d.a == 200 |
| 63 | assert e.a == 200 |
| 64 | } |
| 65 | |
| 66 | interface FilterModel { |
| 67 | model string |
| 68 | } |
| 69 | |
| 70 | interface FilterCar { |
| 71 | name string |
| 72 | } |
| 73 | |
| 74 | struct FilterCarRecord { |
| 75 | name string |
| 76 | model string |
| 77 | } |
| 78 | |
| 79 | fn collect_matching[T](mut cars []FilterCar) []T { |
| 80 | mut matches := []T{} |
| 81 | for mut car in cars { |
| 82 | if mut car is T { |
| 83 | matches << car |
| 84 | } |
| 85 | } |
| 86 | return matches |
| 87 | } |
| 88 | |
| 89 | fn test_generic_interface_to_interface_smartcast() { |
| 90 | mut cars := []FilterCar{} |
| 91 | cars << FilterCarRecord{ |
| 92 | name: 'Roadster' |
| 93 | model: 'Tesla' |
| 94 | } |
| 95 | cars << FilterCarRecord{ |
| 96 | name: 'Corolla' |
| 97 | model: 'Toyota' |
| 98 | } |
| 99 | |
| 100 | models := collect_matching[FilterModel](mut cars) |
| 101 | |
| 102 | assert models.len == 2 |
| 103 | assert models[0].model == 'Tesla' |
| 104 | assert models[1].model == 'Toyota' |
| 105 | } |
| 106 | |