summary refs log tree commit diff stats
path: root/tests/specialops/tdotops.nim
diff options
context:
space:
mode:
Diffstat (limited to 'tests/specialops/tdotops.nim')
-rw-r--r--tests/specialops/tdotops.nim61
1 files changed, 46 insertions, 15 deletions
diff --git a/tests/specialops/tdotops.nim b/tests/specialops/tdotops.nim
index ce5b3942d..ca5eee665 100644
--- a/tests/specialops/tdotops.nim
+++ b/tests/specialops/tdotops.nim
@@ -1,5 +1,6 @@
 discard """
   output: '''
+
 10
 assigning z = 20
 reading field y
@@ -11,9 +12,19 @@ no params call to a
 no params call to b
 100
 one param call to c with 10
-100'''
+100
+0 4
+'''
 """
 
+block:
+  type Foo = object
+  var a: Foo
+  template `.`(a: Foo, b: untyped): untyped = astToStr(b)
+  template callme(a, f): untyped = a.f
+  doAssert callme(a, f2) == "f2" # not `f`
+  doAssert a.callme(f3) == "f3"
+
 type
   T1 = object
     x*: int
@@ -23,16 +34,16 @@ type
   T2 = object
     x: int
 
-proc `.`*(v: T1, f: string): int =
-  echo "reading field ", f
-  return v.x
+template `.`*(v: T1, f: untyped): int =
+  echo "reading field ", astToStr(f)
+  v.x
 
-proc `.=`(x: var T1, f: string{lit}, v: int) =
-  echo "assigning ", f, " = ", v
-  x.x = v
+template `.=`(t: var T1, f: untyped, v: int) =
+  echo "assigning ", astToStr(f), " = ", v
+  t.x = v
 
-template `.()`(x: T1, f: string, args: varargs[expr]): string =
-  echo "call to ", f
+template `.()`(x: T1, f: untyped, args: varargs[typed]): string =
+  echo "call to ", astToStr(f)
   "dot call"
 
 echo ""
@@ -47,13 +58,13 @@ echo t.y()
 var d = TD(t)
 assert(not compiles(d.y))
 
-proc `.`(v: T2, f: string): int =
-  echo "no params call to ", f
-  return v.x
+template `.`(v: T2, f: untyped): int =
+  echo "no params call to ", astToStr(f)
+  v.x
 
-proc `.`*(v: T2, f: string, a: int): int =
-  echo "one param call to ", f, " with ", a
-  return 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)
 
@@ -64,3 +75,23 @@ 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