v2 / vlib / v / tests / comptime / comptime_generic_arg_test.v
43 lines · 38 sloc · 785 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1struct StructType[T] {
2mut:
3 val T
4}
5
6type SumTypes = StructType[string] | []SumTypes | []string | bool | int | string
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 T is $sumtype {
16 $for v in val.variants {
17 if val is v {
18 // dump(typeof(val).name)
19 count.count_chars(val)
20 }
21 }
22 } $else $if T is $struct {
23 count.chars_in_struct(val)
24 } $else {
25 }
26}
27
28// chars_in_struct
29fn (mut count Count) chars_in_struct[T](val T) {
30 $for field in T.fields {
31 va := val.$(field.name)
32 count.count_chars(va)
33 count.count_chars(val.$(field.name))
34 assert true
35 }
36 assert true
37}
38
39fn test_main() {
40 mut count := Count{}
41 count.count_chars(StructType[SumTypes]{ val: '' })
42 assert true
43}
44