diff options
author | Araq <rumpf_a@web.de> | 2014-01-13 02:10:03 +0100 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2014-01-13 02:10:03 +0100 |
commit | 20b5f31c03fb556ec0aa2428a40adbac004d8987 (patch) | |
tree | 58086941e7d6bb8f480ca1173a95722ada9435b2 /tests/gensym | |
parent | 51ee524109cf7e3e86c676bc1676063a01bfd979 (diff) | |
download | Nim-20b5f31c03fb556ec0aa2428a40adbac004d8987.tar.gz |
new tester; all tests categorized
Diffstat (limited to 'tests/gensym')
-rw-r--r-- | tests/gensym/tgensym.nim | 16 | ||||
-rw-r--r-- | tests/gensym/tgensymgeneric.nim | 31 |
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 |