v2 / vlib / v / tests / comptime / comptime_field_name_check_test.v
22 lines · 20 sloc · 299 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1struct Struct {
2 a int
3 txt string
4 array []string
5}
6
7fn get_string[T](s T) string {
8 $for field in T.fields {
9 $if field.name == 'txt' {
10 println(field.name)
11 return s.$(field.name)
12 }
13 }
14 return ''
15}
16
17fn test_main() {
18 s := Struct{
19 txt: 'hello'
20 }
21 assert get_string(s) == 'hello'
22}
23