v2 / vlib / v / tests / comptime / comptime_call_map_receiver_test.v
26 lines · 22 sloc · 418 bytes · d0de08338f8992f11b4b07eb87dafde619f0d8bc
Raw
1type Any = string | int
2
3fn (m map[string]Any) to_toml() string {
4 mut t := ''
5 return t
6}
7
8fn test_main() {
9 mut doc := map[string]Any{}
10 _ := encode(doc)
11}
12
13fn encode[T](typ T) string {
14 $for method in T.methods {
15 $if method.name == 'to_toml' {
16 return typ.$method()
17 }
18 }
19 mp := encode_struct[T](typ)
20 return mp.to_toml()
21}
22
23fn encode_struct[T](typ T) map[string]Any {
24 mut mp := map[string]Any{}
25 return mp
26}
27