Merge pull request #2437 from Araq/underscore-tuple-unpack
Implements #2154.
This commit is contained in:
commit
3e000f4e36
4 changed files with 44 additions and 2 deletions
|
|
@ -61,7 +61,7 @@ type
|
||||||
tkComma, tkSemiColon,
|
tkComma, tkSemiColon,
|
||||||
tkColon, tkColonColon, tkEquals, tkDot, tkDotDot,
|
tkColon, tkColonColon, tkEquals, tkDot, tkDotDot,
|
||||||
tkOpr, tkComment, tkAccent,
|
tkOpr, tkComment, tkAccent,
|
||||||
tkSpaces, tkInfixOpr, tkPrefixOpr, tkPostfixOpr,
|
tkSpaces, tkInfixOpr, tkPrefixOpr, tkPostfixOpr
|
||||||
|
|
||||||
TTokTypes* = set[TTokType]
|
TTokTypes* = set[TTokType]
|
||||||
|
|
||||||
|
|
@ -863,6 +863,10 @@ proc rawGetTok*(L: var TLexer, tok: var TToken) =
|
||||||
of '`':
|
of '`':
|
||||||
tok.tokType = tkAccent
|
tok.tokType = tkAccent
|
||||||
inc(L.bufpos)
|
inc(L.bufpos)
|
||||||
|
of '_':
|
||||||
|
tok.tokType = tkSymbol
|
||||||
|
tok.ident = getIdent("_")
|
||||||
|
inc(L.bufpos)
|
||||||
of '\"':
|
of '\"':
|
||||||
# check for extended raw string literal:
|
# check for extended raw string literal:
|
||||||
var rawMode = L.bufpos > 0 and L.buf[L.bufpos-1] in SymChars
|
var rawMode = L.bufpos > 0 and L.buf[L.bufpos-1] in SymChars
|
||||||
|
|
|
||||||
|
|
@ -369,6 +369,10 @@ proc addToVarSection(c: PContext; result: var PNode; orig, identDefs: PNode) =
|
||||||
else:
|
else:
|
||||||
result.add identDefs
|
result.add identDefs
|
||||||
|
|
||||||
|
proc isDiscardUnderscore(n: PNode): bool =
|
||||||
|
if n.kind != nkIdent: return false
|
||||||
|
return n.ident.s == "_"
|
||||||
|
|
||||||
proc semVarOrLet(c: PContext, n: PNode, symkind: TSymKind): PNode =
|
proc semVarOrLet(c: PContext, n: PNode, symkind: TSymKind): PNode =
|
||||||
var b: PNode
|
var b: PNode
|
||||||
result = copyNode(n)
|
result = copyNode(n)
|
||||||
|
|
@ -432,7 +436,10 @@ proc semVarOrLet(c: PContext, n: PNode, symkind: TSymKind): PNode =
|
||||||
|
|
||||||
for j in countup(0, length-3):
|
for j in countup(0, length-3):
|
||||||
var v = semIdentDef(c, a.sons[j], symkind)
|
var v = semIdentDef(c, a.sons[j], symkind)
|
||||||
if sfGenSym notin v.flags: addInterfaceDecl(c, v)
|
if sfGenSym notin v.flags and
|
||||||
|
not isDiscardUnderscore(a.sons[j]): addInterfaceDecl(c, v)
|
||||||
|
if isDiscardUnderscore(a.sons[j]):
|
||||||
|
v.flags.incl(sfGenSym)
|
||||||
when oKeepVariableNames:
|
when oKeepVariableNames:
|
||||||
if c.inUnrolledContext > 0: v.flags.incl(sfShadowed)
|
if c.inUnrolledContext > 0: v.flags.incl(sfShadowed)
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
29
tests/parser/ttupleunpack.nim
Normal file
29
tests/parser/ttupleunpack.nim
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
discard """
|
||||||
|
file: "ttupleunpack.nim"
|
||||||
|
output: ""
|
||||||
|
exitcode: 0
|
||||||
|
"""
|
||||||
|
|
||||||
|
proc main() =
|
||||||
|
|
||||||
|
proc foo(): tuple[x, y, z: int] =
|
||||||
|
return (4, 2, 3)
|
||||||
|
|
||||||
|
var (x, _, y) = foo()
|
||||||
|
doAssert x == 4
|
||||||
|
doAssert y == 3
|
||||||
|
|
||||||
|
var (a, _, _) = foo()
|
||||||
|
doAssert a == 4
|
||||||
|
|
||||||
|
var (a, _, _xx) = foo()
|
||||||
|
doAssert a == 4
|
||||||
|
|
||||||
|
iterator bar(): tuple[x, y, z: int] =
|
||||||
|
yield (1,2,3)
|
||||||
|
|
||||||
|
for x, y, _ in bar():
|
||||||
|
doAssert x == 1
|
||||||
|
doAssert y == 2
|
||||||
|
|
||||||
|
main()
|
||||||
|
|
@ -129,6 +129,8 @@ News
|
||||||
of the assignment operator has arrived!
|
of the assignment operator has arrived!
|
||||||
- ``system.len`` for strings and sequences now returns 0 for nil.
|
- ``system.len`` for strings and sequences now returns 0 for nil.
|
||||||
|
|
||||||
|
- A single underscore can now be used to discard values when unpacking tuples.
|
||||||
|
|
||||||
|
|
||||||
Library additions
|
Library additions
|
||||||
-----------------
|
-----------------
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue