summary refs log tree commit diff stats
path: root/tests/concepts/tstackconcept.nim
diff options
context:
space:
mode:
Diffstat (limited to 'tests/concepts/tstackconcept.nim')
-rw-r--r--tests/concepts/tstackconcept.nim49
1 files changed, 49 insertions, 0 deletions
diff --git a/tests/concepts/tstackconcept.nim b/tests/concepts/tstackconcept.nim
new file mode 100644
index 000000000..3993ca534
--- /dev/null
+++ b/tests/concepts/tstackconcept.nim
@@ -0,0 +1,49 @@
+discard """
+output: "20\n10"
+msg: '''
+INFERRED int
+'''
+"""
+
+import typetraits
+
+template reject(e: expr) =
+  static: assert(not compiles(e))
+
+type
+  ArrayStack = object
+    data: seq[int]
+
+proc push(s: var ArrayStack, item: int) =
+  s.data.add item
+
+proc pop(s: var ArrayStack): int =
+  return s.data.pop()
+
+type
+  Stack[T] = concept var s
+    s.push(T)
+    s.pop() is T
+
+proc genericAlgorithm[T](s: var Stack[T], y: T) =
+  static: echo "INFERRED ", T.name
+
+  s.push(y)
+  echo s.pop
+
+proc implicitGeneric(s: var Stack): auto =
+  # static: echo "IMPLICIT INFERRED ", s.T.name, " ", Stack.T.name
+
+  return s.pop()
+
+var s = ArrayStack(data: @[])
+
+s.push 10
+s.genericAlgorithm 20
+echo s.implicitGeneric
+
+reject s.genericAlgorithm "x"
+reject s.genericAlgorithm 1.0
+reject "str".implicitGeneric
+reject implicitGeneric(10)
+