v2 / vlib / v / tests / comptime / comptime_selector_generic_arg_test.v
40 lines · 36 sloc · 673 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1struct Struc {
2 a string
3 b int
4 c ?string
5 d ?int
6}
7
8pub struct Count {
9mut:
10 total int
11}
12
13// count_chars count json sizen without new encode
14pub fn (mut count Count) count_chars[T](val T) {
15 $if val is $option {
16 workaround := val
17 if workaround != none {
18 count.count_chars(val)
19 }
20 } $else $if T is $struct {
21 count.chars_in_struct(val)
22 } $else {
23 }
24}
25
26// chars_in_struct
27fn (mut count Count) chars_in_struct[T](val T) {
28 $for field in T.fields {
29 va := val.$(field.name)
30 count.count_chars(va) // works
31 count.count_chars(val.$(field.name)) // Not works
32 }
33}
34
35fn test_main() {
36 struc := Struc{}
37 mut count := Count{}
38 count.count_chars(struc)
39 assert true
40}
41