Revert "Revert "test cases for the new features""
This reverts commit e1b668c868.
This commit is contained in:
parent
50403afb5c
commit
f127bc387a
4 changed files with 78 additions and 4 deletions
|
|
@ -111,7 +111,11 @@ proc semSym(c: PContext, n: PNode, s: PSym, flags: TExprFlags): PNode =
|
||||||
# var len = 0 # but won't be called
|
# var len = 0 # but won't be called
|
||||||
# genericThatUsesLen(x) # marked as taking a closure?
|
# genericThatUsesLen(x) # marked as taking a closure?
|
||||||
of skGenericParam:
|
of skGenericParam:
|
||||||
if s.ast != nil: result = semExpr(c, s.ast)
|
if s.typ.kind == tyExpr:
|
||||||
|
result = newSymNode(s, n.info)
|
||||||
|
result.typ = s.typ.lastSon
|
||||||
|
elif s.ast != nil:
|
||||||
|
result = semExpr(c, s.ast)
|
||||||
else:
|
else:
|
||||||
InternalError(n.info, "no default for")
|
InternalError(n.info, "no default for")
|
||||||
result = emptyNode
|
result = emptyNode
|
||||||
|
|
@ -888,10 +892,13 @@ proc builtinFieldAccess(c: PContext, n: PNode, flags: TExprFlags): PNode =
|
||||||
let tbody = ty.sons[0]
|
let tbody = ty.sons[0]
|
||||||
for s in countup(0, tbody.len-2):
|
for s in countup(0, tbody.len-2):
|
||||||
let tParam = tbody.sons[s]
|
let tParam = tbody.sons[s]
|
||||||
assert tParam.kind == tyGenericParam
|
|
||||||
if tParam.sym.name == i:
|
if tParam.sym.name == i:
|
||||||
let foundTyp = makeTypeDesc(c, ty.sons[s + 1])
|
let rawTyp = ty.sons[s + 1]
|
||||||
return newSymNode(copySym(tParam.sym).linkTo(foundTyp), n.info)
|
if rawTyp.kind == tyExpr:
|
||||||
|
return rawTyp.n
|
||||||
|
else:
|
||||||
|
let foundTyp = makeTypeDesc(c, rawTyp)
|
||||||
|
return newSymNode(copySym(tParam.sym).linkTo(foundTyp), n.info)
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
# echo "TYPE FIELD ACCESS"
|
# echo "TYPE FIELD ACCESS"
|
||||||
|
|
|
||||||
29
tests/compile/tgenericdefaults.nim
Normal file
29
tests/compile/tgenericdefaults.nim
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
type
|
||||||
|
TFoo[T, U, R = int] = object
|
||||||
|
x: T
|
||||||
|
y: U
|
||||||
|
z: R
|
||||||
|
|
||||||
|
TBar[T] = TFoo[T, array[4, T], T]
|
||||||
|
|
||||||
|
var x1: TFoo[int, float]
|
||||||
|
|
||||||
|
static:
|
||||||
|
assert type(x1.x) is int
|
||||||
|
assert type(x1.y) is float
|
||||||
|
assert type(x1.z) is int
|
||||||
|
|
||||||
|
var x2: TFoo[string, R = float, U = seq[int]]
|
||||||
|
|
||||||
|
static:
|
||||||
|
assert type(x2.x) is string
|
||||||
|
assert type(x2.y) is seq[int]
|
||||||
|
assert type(x2.z) is float
|
||||||
|
|
||||||
|
var x3: TBar[float]
|
||||||
|
|
||||||
|
static:
|
||||||
|
assert type(x3.x) is float
|
||||||
|
assert type(x3.y) is array[4, float]
|
||||||
|
assert type(x3.z) is float
|
||||||
|
|
||||||
|
|
@ -5,6 +5,8 @@ type
|
||||||
T1 = expr
|
T1 = expr
|
||||||
T2 = expr
|
T2 = expr
|
||||||
|
|
||||||
|
Numeric = int|float
|
||||||
|
|
||||||
proc takesExpr(x, y) =
|
proc takesExpr(x, y) =
|
||||||
echo x, y
|
echo x, y
|
||||||
|
|
||||||
|
|
@ -32,3 +34,22 @@ takesFoo(f, f)
|
||||||
takes2Types(1, 1, "string")
|
takes2Types(1, 1, "string")
|
||||||
takes2Types[string, int]("test", "test", 1)
|
takes2Types[string, int]("test", "test", 1)
|
||||||
|
|
||||||
|
proc takesSeq(x: seq) =
|
||||||
|
echo "seq"
|
||||||
|
|
||||||
|
takesSeq(@[1, 2, 3])
|
||||||
|
takesSeq(@["x", "y", "z"])
|
||||||
|
|
||||||
|
proc takesSeqOfFoos(x: seq[TFoo]) =
|
||||||
|
echo "foo seq"
|
||||||
|
|
||||||
|
var sf = newSeq[TFoo[int]](3)
|
||||||
|
|
||||||
|
takesSeq(sf)
|
||||||
|
takesSeqOfFoos(sf)
|
||||||
|
|
||||||
|
proc takesFooOfNumeric(x: TFoo[Numeric]) =
|
||||||
|
echo "foo of numeric"
|
||||||
|
|
||||||
|
takesFooOfNumeric(sf[0])
|
||||||
|
|
||||||
|
|
|
||||||
17
tests/run/tstaticparams.nim
Normal file
17
tests/run/tstaticparams.nim
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
discard """
|
||||||
|
file: "tstaticparams.nim"
|
||||||
|
output: "abracadabra\ntest"
|
||||||
|
"""
|
||||||
|
|
||||||
|
type
|
||||||
|
TFoo[T; Val: expr[string]] = object
|
||||||
|
data: array[4, T]
|
||||||
|
|
||||||
|
proc takeFoo(x: TFoo) =
|
||||||
|
echo "abracadabra"
|
||||||
|
echo TFoo.Val
|
||||||
|
|
||||||
|
var x: TFoo[int, "test"]
|
||||||
|
|
||||||
|
takeFoo(x)
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue