mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-06 20:04:18 +00:00
'is' operator for generic code
This commit is contained in:
@@ -204,7 +204,6 @@ proc semSizeof(c: PContext, n: PNode): PNode =
|
||||
|
||||
proc semOf(c: PContext, n: PNode): PNode =
|
||||
if sonsLen(n) == 3:
|
||||
#LocalError(n.info, errXcanNeverBeOfThisSubtype, " CAME HERE")
|
||||
n.sons[1] = semExprWithType(c, n.sons[1], {efAllowType})
|
||||
n.sons[2] = semExprWithType(c, n.sons[2], {efAllowType})
|
||||
var a = skipTypes(n.sons[1].typ, abstractPtrs)
|
||||
@@ -220,22 +219,14 @@ proc semOf(c: PContext, n: PNode): PNode =
|
||||
result = n
|
||||
|
||||
proc semIs(c: PContext, n: PNode): PNode =
|
||||
GlobalError(n.info, errXExpectsTwoArguments, "is")
|
||||
if sonsLen(n) == 3:
|
||||
#LocalError(n.info, errXcanNeverBeOfThisSubtype, " CAME HERE")
|
||||
n.sons[1] = semExprWithType(c, n.sons[1], {efAllowType})
|
||||
n.sons[2] = semExprWithType(c, n.sons[2], {efAllowType})
|
||||
var a = skipTypes(n.sons[1].typ, abstractPtrs)
|
||||
var b = skipTypes(n.sons[2].typ, abstractPtrs)
|
||||
if b.kind != tyObject or a.kind != tyObject:
|
||||
GlobalError(n.info, errXExpectsObjectTypes, "is")
|
||||
while b != nil and b.id != a.id: b = b.sons[0]
|
||||
if b == nil:
|
||||
GlobalError(n.info, errXcanNeverBeOfThisSubtype, typeToString(a))
|
||||
n.typ = getSysType(tyBool)
|
||||
else:
|
||||
if sonsLen(n) == 3:
|
||||
var a = semExprWithType(c, n.sons[1], {efAllowType})
|
||||
var b = semExprWithType(c, n.sons[2], {efAllowType})
|
||||
result = newIntNode(nkIntLit, ord(sameType(a.typ, b.typ)))
|
||||
result.typ = getSysType(tyBool)
|
||||
result.info = n.info
|
||||
else:
|
||||
GlobalError(n.info, errXExpectsTwoArguments, "is")
|
||||
result = n
|
||||
|
||||
proc semOpAux(c: PContext, n: PNode) =
|
||||
for i in countup(1, sonsLen(n) - 1):
|
||||
|
||||
@@ -348,14 +348,14 @@ proc getConstExpr(m: PSym, n: PNode): PNode =
|
||||
of nkIfExpr:
|
||||
result = getConstIfExpr(m, n)
|
||||
of nkCall, nkCommand, nkCallStrLit:
|
||||
if (n.sons[0].kind != nkSym): return
|
||||
if n.sons[0].kind != nkSym: return
|
||||
var s = n.sons[0].sym
|
||||
if (s.kind != skProc): return
|
||||
try:
|
||||
if s.kind != skProc: return
|
||||
try:
|
||||
case s.magic
|
||||
of mNone:
|
||||
of mNone:
|
||||
return # XXX: if it has no sideEffect, it should be evaluated
|
||||
of mSizeOf:
|
||||
of mSizeOf:
|
||||
var a = n.sons[1]
|
||||
if computeSize(a.typ) < 0:
|
||||
LocalError(a.info, errCannotEvalXBecauseIncompletelyDefined,
|
||||
|
||||
13
tests/accept/run/tisopr.nim
Normal file
13
tests/accept/run/tisopr.nim
Normal file
@@ -0,0 +1,13 @@
|
||||
discard """
|
||||
output: "true true false yes"
|
||||
"""
|
||||
|
||||
proc IsVoid[T](): string =
|
||||
when T is void:
|
||||
result = "yes"
|
||||
else:
|
||||
result = "no"
|
||||
|
||||
const x = int is int
|
||||
echo x, " ", float is float, " ", float is string, " ", IsVoid[void]()
|
||||
|
||||
Reference in New Issue
Block a user