|
| 1 | +// This tests, whether type aliased values, can be used in place of their non aliased versions, |
| 2 | +// when implementing interfaces. This is important for allowing type aliases in one module, |
| 3 | +// to be used as substitutes to keep backwards compatibility, when migrating functionality |
| 4 | +// from one module to another, which was the case, when deprecating `gx` in favor of moving |
| 5 | +// its functionality into `gg`, while still keeping old code using `gx` compilable for a while, |
| 6 | +// by making `gx` import `gg` and declare a few shallow type aliases to the new `gg` types, |
| 7 | +// so that older `gx` importers and their CIs (like the `ui` module), can continue to work |
| 8 | +// (with just deprecation notices), without forcing an immediate change. |
| 9 | +struct Param1 { |
| 10 | + x int |
| 11 | +} |
| 12 | + |
| 13 | +struct Param2 { |
| 14 | + y f32 |
| 15 | +} |
| 16 | + |
| 17 | +type AliasParam1 = Param1 |
| 18 | +type AliasParam2 = Param2 |
| 19 | + |
| 20 | +interface MyInterface { |
| 21 | + method1(p1 Param1, p2 Param2) f32 |
| 22 | +} |
| 23 | + |
| 24 | +struct ImplDirect { |
| 25 | + v int |
| 26 | +} |
| 27 | + |
| 28 | +fn (i ImplDirect) method1(p1 Param1, p2 Param2) f32 { |
| 29 | + println(i) |
| 30 | + println(p1) |
| 31 | + println(p2) |
| 32 | + return i.v + p1.x + p2.y |
| 33 | +} |
| 34 | + |
| 35 | +struct ImplWithAliasedParams { |
| 36 | + v int |
| 37 | +} |
| 38 | + |
| 39 | +fn (i ImplWithAliasedParams) method1(p1 AliasParam1, p2 AliasParam2) f32 { |
| 40 | + println(i) |
| 41 | + println(p1) |
| 42 | + println(p2) |
| 43 | + return i.v + p1.x + p2.y |
| 44 | +} |
| 45 | + |
| 46 | +fn test_interface_method_can_be_called_with_aliased_type_values() { |
| 47 | + isdirect := MyInterface(ImplDirect{1000}) |
| 48 | + isalias := MyInterface(ImplWithAliasedParams{2000}) |
| 49 | + |
| 50 | + assert isdirect.method1(AliasParam1{123}, AliasParam2{1.1}) == 1124.1 |
| 51 | + assert isalias.method1(AliasParam1{456}, AliasParam2{2.2}) == 2458.2 |
| 52 | +} |
0 commit comments