v2 / vlib / v / tests / comptime / comptime_selector_assign_test.v
25 lines · 20 sloc · 368 bytes · 5566df56f12f7f7c9c1ceab67856841208527ef1
Raw
1module main
2
3struct AnyStruct[T] {
4 val T
5}
6
7fn decode_struct[T]() T {
8 mut typ := T{}
9 $for field in T.fields {
10 typ.$(field.name) = decode_field(typ.$(field.name))
11 }
12 return typ
13}
14
15fn decode_field[T](_ T) T {
16 mut field := T{}
17 return field
18}
19
20type Any = int | string | []Any
21
22fn test_main() {
23 decode_struct[AnyStruct[Any]]()
24 decode_struct[AnyStruct[[]Any]]()
25}
26