| 1 | fn encode[T](values T) string { |
| 2 | $if T is $pointer { |
| 3 | if voidptr(values) == unsafe { nil } { |
| 4 | return 'nil' |
| 5 | } |
| 6 | return encode_pointer_struct(values) |
| 7 | } |
| 8 | return 'other' |
| 9 | } |
| 10 | |
| 11 | fn encode_pointer_struct[T](values T) string { |
| 12 | $for field in T.fields { |
| 13 | dump('${field.name}') // works `name` |
| 14 | mut val := values.$(field.name) // C error `root` |
| 15 | dump('${val}') |
| 16 | } |
| 17 | return 'struct' |
| 18 | } |
| 19 | |
| 20 | struct Node { |
| 21 | name string |
| 22 | children []&Node |
| 23 | } |
| 24 | |
| 25 | fn test_main() { |
| 26 | assert encode('string') == 'other' |
| 27 | assert encode(&Node(unsafe { nil })) == 'nil' |
| 28 | assert encode(&Node{ name: 'root' }) == 'struct' |
| 29 | } |
| 30 | |