v2 / vlib / v / tests / fns / fn_call_comptime_array_arg_test.v
30 lines · 27 sloc · 480 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1struct IntArray {
2 a []int
3}
4
5fn encode_array[U](val []U) []string {
6 mut out := []string{}
7 $if U is int {
8 out << 'is int'
9 } $else $if U is $struct {
10 out << 'is struct'
11 }
12 return out
13}
14
15fn encode_struct[U](val U) ?[]string {
16 $for field in U.fields {
17 if field.is_array {
18 value := val.$(field.name)
19 return encode_array(value)
20 }
21 }
22 return none
23}
24
25fn test_main() {
26 int_array := IntArray{
27 a: [1, 2, 3]
28 }
29 assert encode_struct(int_array)?.str() == "['is int']"
30}
31