concept type params inference working in basic examples
This commit is contained in:
parent
0d8a503e45
commit
66e0f0e19c
5 changed files with 93 additions and 2 deletions
49
tests/concepts/tstackconcept.nim
Normal file
49
tests/concepts/tstackconcept.nim
Normal file
|
|
@ -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)
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue