v2 / vlib / v / tests / comptime / comptime_println_test.v
27 lines · 25 sloc · 451 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1struct Foo {
2 a int
3 b f32
4 c string
5}
6
7fn print_struct[T](x &T) []string {
8 mut a := []string{}
9 $for field in T.fields {
10 typ := typeof((*x).$(field.name)).name
11 val := (*x).$(field.name)
12 a << '${field.name} (${typ}) = ${val}'
13 }
14 return a
15}
16
17fn test_main() {
18 foo := Foo{
19 a: 123
20 b: 4.56
21 c: 'hello'
22 }
23 out := print_struct(&foo)
24 assert out[0] == 'a (int) = 123'
25 assert out[1] == 'b (f32) = 4.56'
26 assert out[2] == 'c (string) = hello'
27}
28