v2 / vlib / v / tests / comptime / comptime_is_interface_check_test.v
37 lines · 31 sloc · 547 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1module main
2
3interface TestInterface {
4 test_func() f64
5}
6
7struct Struct1 {
8 num f64
9}
10
11fn (o &Struct1) test_func() f64 {
12 return o.num
13}
14
15struct Struct2 {
16mut:
17 s1 ?&Struct1
18}
19
20fn do_thing[T](s1 Struct1) T {
21 mut t := T{}
22 t.s1 = &s1
23 $for field in T.fields {
24 $if field.typ is TestInterface {
25 i := TestInterface(t.$(field.name))
26 assert false
27 } $else $if field.typ is ?TestInterface {
28 i := TestInterface(t.$(field.name) ?)
29 assert true
30 }
31 }
32 return t
33}
34
35fn test_main() {
36 assert do_thing[Struct2](Struct1{1.23}).s1?.num == 1.23
37}
38