v2 / vlib / v / tests / casts / cast_interface_to_impl_test.v
43 lines · 35 sloc · 647 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1interface TheInterfaceItself {
2 f() int
3}
4
5struct SomeImplForTII1 {}
6
7fn (_ SomeImplForTII1) f() int {
8 return 2
9}
10
11fn (_ SomeImplForTII1) secret1() int {
12 return 42
13}
14
15struct AnotherImplementation {}
16
17fn (_ AnotherImplementation) f() int {
18 return 8
19}
20
21fn (_ AnotherImplementation) secret2() int {
22 return 84
23}
24
25fn 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
40fn test_casting_to_impl() {
41 assert h(SomeImplForTII1{}, 0) == 44
42 assert h(AnotherImplementation{}, 1) == 92
43}
44