| 1 | struct OtherStruct { |
| 2 | test string |
| 3 | } |
| 4 | |
| 5 | struct I32Struct { |
| 6 | test i32 |
| 7 | } |
| 8 | |
| 9 | struct U32Struct { |
| 10 | test u32 |
| 11 | } |
| 12 | |
| 13 | fn 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 | |
| 31 | fn 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 | |