summary refs log tree commit diff stats
path: root/tests/gensym
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2014-01-13 02:10:03 +0100
committerAraq <rumpf_a@web.de>2014-01-13 02:10:03 +0100
commit20b5f31c03fb556ec0aa2428a40adbac004d8987 (patch)
tree58086941e7d6bb8f480ca1173a95722ada9435b2 /tests/gensym
parent51ee524109cf7e3e86c676bc1676063a01bfd979 (diff)
downloadNim-20b5f31c03fb556ec0aa2428a40adbac004d8987.tar.gz
new tester; all tests categorized
Diffstat (limited to 'tests/gensym')
-rw-r--r--tests/gensym/tgensym.nim16
-rw-r--r--tests/gensym/tgensymgeneric.nim31
2 files changed, 47 insertions, 0 deletions
diff --git a/tests/gensym/tgensym.nim b/tests/gensym/tgensym.nim
new file mode 100644
index 000000000..3c85b0b83
--- /dev/null
+++ b/tests/gensym/tgensym.nim
@@ -0,0 +1,16 @@
+discard """
+  output: "123100"
+"""
+
+template hygienic(val: expr) =
+  var x = val
+  stdout.write x
+
+var x = 100
+
+hygienic 1
+hygienic 2
+hygienic 3
+
+echo x
+
diff --git a/tests/gensym/tgensymgeneric.nim b/tests/gensym/tgensymgeneric.nim
new file mode 100644
index 000000000..54390a4ef
--- /dev/null
+++ b/tests/gensym/tgensymgeneric.nim
@@ -0,0 +1,31 @@
+# We need to open the gensym'ed symbol again so that the instantiation
+# creates a fresh copy; but this is wrong the very first reason for gensym
+# is that scope rules cannot be used! So simply removing 'sfGenSym' does
+# not work. Copying the symbol does not work either because we're already
+# the owner of the symbol! What we need to do is to copy the symbol
+# in the generic instantiation process...
+
+type
+  TA = object
+    x: int
+  TB = object
+    x: string
+
+template genImpl() =
+  var gensymed: T
+  when T is TB:
+    gensymed.x = "abc"
+  else:
+    gensymed.x = 123
+  shallowCopy(result, gensymed)
+
+proc gen[T](x: T): T =
+  genImpl()
+
+var
+  a: TA
+  b: TB
+let x = gen(a)
+let y = gen(b)
+
+echo x.x, " ", y.x