| 1 | interface TheInterfaceItself { |
| 2 | f() int |
| 3 | } |
| 4 | |
| 5 | struct SomeImplForTII1 {} |
| 6 | |
| 7 | fn (_ SomeImplForTII1) f() int { |
| 8 | return 2 |
| 9 | } |
| 10 | |
| 11 | fn (_ SomeImplForTII1) secret1() int { |
| 12 | return 42 |
| 13 | } |
| 14 | |
| 15 | struct AnotherImplementation {} |
| 16 | |
| 17 | fn (_ AnotherImplementation) f() int { |
| 18 | return 8 |
| 19 | } |
| 20 | |
| 21 | fn (_ AnotherImplementation) secret2() int { |
| 22 | return 84 |
| 23 | } |
| 24 | |
| 25 | fn h(foo TheInterfaceItself, i int) int { |
| 26 | j := foo.f() |
| 27 | match i { |
| 28 | 0 { |
| 29 | return j + (foo as SomeImplForTII1).secret1() |
| 30 | } |
| 31 | 1 { |
| 32 | return j + (foo as AnotherImplementation).secret2() |
| 33 | } |
| 34 | else { |
| 35 | return 1 |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | fn test_casting_to_impl() { |
| 41 | assert h(SomeImplForTII1{}, 0) == 44 |
| 42 | assert h(AnotherImplementation{}, 1) == 92 |
| 43 | } |
| 44 | |