| 1 | struct FixedStruct1 { |
| 2 | a int |
| 3 | b string |
| 4 | c ?int |
| 5 | d ?string |
| 6 | } |
| 7 | |
| 8 | struct Encoder {} |
| 9 | |
| 10 | fn test_main() { |
| 11 | fixed := FixedStruct1{123, '456', 789, '321'} |
| 12 | // this work well |
| 13 | println(fixed.a.str()) |
| 14 | println(fixed.c?.str()) |
| 15 | |
| 16 | println(fixed.b.int()) |
| 17 | println(fixed.d?.int()) |
| 18 | |
| 19 | e := Encoder{} |
| 20 | // this not work |
| 21 | e.encode_struct(fixed) |
| 22 | } |
| 23 | |
| 24 | fn (e &Encoder) encode_struct[T](val T) { |
| 25 | mut count := 0 |
| 26 | $for field in T.fields { |
| 27 | mut value := val.$(field.name) |
| 28 | $if field.is_option { |
| 29 | if field.name in ['c', 'd'] { |
| 30 | assert true |
| 31 | } |
| 32 | println('>> ${value ?.str()}') |
| 33 | println(val.$(field.name) ?.str()) |
| 34 | count += 1 |
| 35 | } |
| 36 | } |
| 37 | assert count == 2 |
| 38 | } |
| 39 | |