From 20b5f31c03fb556ec0aa2428a40adbac004d8987 Mon Sep 17 00:00:00 2001 From: Araq Date: Mon, 13 Jan 2014 02:10:03 +0100 Subject: new tester; all tests categorized --- tests/gensym/tgensym.nim | 16 ++++++++++++++++ tests/gensym/tgensymgeneric.nim | 31 +++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 tests/gensym/tgensym.nim create mode 100644 tests/gensym/tgensymgeneric.nim (limited to 'tests/gensym') 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 -- cgit 1.4.1-2-gfad0