v2 / vlib / v / tests / comptime / comptime_method_test.v
85 lines · 73 sloc · 1.21 KB · 6488041a749df9762348d019c4223908c476f2e2
Raw
1struct Test {}
2
3struct Test2 {}
4
5fn (t Test) t_0() ?int {
6 return none
7}
8
9fn (t Test) t_1() int {
10 return 0
11}
12
13fn (t Test) t_2() bool {
14 return true
15}
16
17fn (t Test) t_3() Test {
18 return t
19}
20
21fn test_main() {
22 t := Test{}
23 assert t_bool(t, 't_2')? == true
24 assert t_int(t, 't_1')? == 0
25 t_struct(t, 't_3')
26}
27
28fn t_bool[T](val T, method_name string) ?bool {
29 $for f in T.methods {
30 if method_name == f.name {
31 $if f.return_type is bool {
32 return val.$method()
33 }
34 $if f.return_type is int {
35 val.$method()
36 }
37 }
38 }
39 return none
40}
41
42fn t_int[T](val T, method_name string) ?int {
43 $for f in T.methods {
44 if method_name == f.name {
45 $if f.return_type is bool {
46 val.$method()
47 }
48 $if f.return_type is int {
49 return val.$method()
50 }
51 }
52 }
53 return none
54}
55
56fn t_struct[T](val T, method_name string) {
57 mut s := map[string]string{}
58
59 $for f in T.methods {
60 $if f.return_type is Test2 {
61 b := s[f.name] or {
62 println('z')
63 ''
64 }
65 dump(b)
66 }
67 $if f.return_type is Test {
68 c := val.$method() or { Test{} }
69 dump(c)
70
71 s[f.name] = f.name
72 dump(s)
73 assert f.name == 't_3'
74 assert s[f.name] == 't_3'
75
76 b := s[f.name] or {
77 println('z')
78 ''
79 }
80 dump(b)
81 return
82 }
83 }
84 assert false
85}
86