1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
discard """
output: '''
10
assigning z = 20
reading field y
20
call to y
dot call
no params call to a
100
no params call to b
100
one param call to c with 10
100
0 4
'''
"""
type
T1 = object
x*: int
TD = distinct T1
T2 = object
x: int
template `.`*(v: T1, f: untyped): int =
echo "reading field ", astToStr(f)
v.x
template `.=`(t: var T1, f: untyped, v: int) =
echo "assigning ", astToStr(f), " = ", v
t.x = v
template `.()`(x: T1, f: untyped, args: varargs[typed]): string =
echo "call to ", astToStr(f)
"dot call"
echo ""
var t = T1(x: 10)
echo t.x
t.z = 20
echo t.y
echo t.y()
var d = TD(t)
assert(not compiles(d.y))
template `.`(v: T2, f: untyped): int =
echo "no params call to ", astToStr(f)
v.x
template `.`*(v: T2, f: untyped, a: int): int =
echo "one param call to ", astToStr(f), " with ", a
v.x
var tt = T2(x: 100)
echo tt.a
echo tt.b()
echo tt.c(10)
assert(not compiles(tt.d("x")))
assert(not compiles(tt.d(1, 2)))
# test simple usage that delegates fields:
type
Other = object
a: int
b: string
MyObject = object
nested: Other
x, y: int
template `.`(x: MyObject; field: untyped): untyped =
x.nested.field
template `.=`(x: MyObject; field, value: untyped) =
x.nested.field = value
var m: MyObject
m.a = 4
m.b = "foo"
echo m.x, " ", m.a
|