summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorZahary Karadjov <zahary@gmail.com>2011-11-16 02:18:10 +0200
committerZahary Karadjov <zahary@gmail.com>2011-11-18 02:11:15 +0200
commit12bac28d23ab21879a0f40fc7b2b2c875be90f82 (patch)
tree4ae6e5029753950e3124ebcc0dff08575df50cfe /tests
parentecd3c80e7eadbb3db9a8acdc3bb37b6f92e9b66b (diff)
downloadNim-12bac28d23ab21879a0f40fc7b2b2c875be90f82.tar.gz
macros and templates can be expanded anywhere where a type is expected.
This allows for various type selection algorithms to be implemented.
See tests / accept / compile / ttypeselectors.nim for examples.
Diffstat (limited to 'tests')
-rw-r--r--tests/accept/compile/ttypeselectors.nim39
1 files changed, 39 insertions, 0 deletions
diff --git a/tests/accept/compile/ttypeselectors.nim b/tests/accept/compile/ttypeselectors.nim
new file mode 100644
index 000000000..1cc4b02b7
--- /dev/null
+++ b/tests/accept/compile/ttypeselectors.nim
@@ -0,0 +1,39 @@
+import macros
+
+template selectType(x: int): typeDesc =
+  when x < 10:
+    int
+  else:
+    string
+
+template simpleTypeTempl: typeDesc =
+  string
+
+macro typeFromMacro(s: expr): typeDesc =
+  result = newNimNode(nnkIdent)
+  result.ident = !"string"
+  # result = newIdentNode"string"
+  
+proc t1*(x: int): simpleTypeTempl() =
+  result = "test"
+
+proc t2*(x: int): selectType(100) =
+  result = "test"
+
+proc t3*(x: int): selectType(1) =
+  result = 10
+
+proc t4*(x: int): typeFromMacro() =
+  result = "test"
+
+var x*: selectType(50) = "test"
+
+proc t5*(x: selectType(5)) =
+  var y = x + 10
+  echo y
+
+var y*: type(t2(100)) = "test"
+
+proc t6*(x: type(t3(0))): type(t1(0)) =
+  result = $x
+