v2 / vlib / v / tests / comptime / comptime_selector_member_test.v
36 lines · 32 sloc · 736 bytes · be3149183489055fae45eb9e59b657df15da060c
Raw
1struct OtherStruct {
2 test string
3}
4
5struct I32Struct {
6 test i32
7}
8
9struct U32Struct {
10 test u32
11}
12
13fn test[T](val T) string {
14 $if val is $struct {
15 println(T.name)
16 $for attribute in T.fields {
17 $if attribute.name == 'test' {
18 $if val.test in [u32, i32] {
19 return 'struct field ${typeof(val.test).name}'
20 } $else {
21 return 'not u32 or i32 struct field, got type: ${typeof(val.test).name}'
22 }
23 }
24 }
25 return 'no test field in struct'
26 } $else {
27 return 'else block'
28 }
29}
30
31fn test_main() {
32 assert test(u32(7)) == 'else block'
33 assert test(OtherStruct{'7'}) == 'not u32 or i32 struct field, got type: string'
34 assert test(I32Struct{-7}) == 'struct field i32'
35 assert test(U32Struct{7}) == 'struct field u32'
36}
37