Skip to content

Commit

Permalink
underscores for routine parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Dec 28, 2022
1 parent 7a74c2d commit 6fce8b5
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
5 changes: 3 additions & 2 deletions compiler/semtempl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -642,8 +642,9 @@ proc semTemplateDef(c: PContext, n: PNode): PNode =
# body by the absence of the sfGenSym flag:
for i in 1..<s.typ.n.len:
let param = s.typ.n[i].sym
param.flags.incl sfTemplateParam
param.flags.excl sfGenSym
if param.name.s != "_":
param.flags.incl sfTemplateParam
param.flags.excl sfGenSym
if param.typ.kind != tyUntyped: allUntyped = false
else:
s.typ = newTypeS(tyProc, c)
Expand Down
4 changes: 3 additions & 1 deletion compiler/semtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1372,7 +1372,9 @@ proc semProcTypeNode(c: PContext, n, genericParams: PNode,
inc(counter)
if def != nil and def.kind != nkEmpty:
arg.ast = copyTree(def)
if containsOrIncl(check, arg.name.id):
if arg.name.s == "_":
arg.flags.incl(sfGenSym)
elif containsOrIncl(check, arg.name.id):
localError(c.config, a[j].info, "attempt to redefine: '" & arg.name.s & "'")
result.n.add newSymNode(arg)
rawAddSon(result, finalType)
Expand Down
78 changes: 78 additions & 0 deletions tests/proc/tunderscoreparam.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
discard """
targets: "c cpp js"
"""

import std/[assertions, sequtils]

proc test() =
block:
proc ok(_, _, a: int): int =
doAssert not compiles(_)
a
doassert ok(4, 2, 5) == 5

block:
proc ok(_: int, _: int, a: int): int = a
doAssert ok(4, 2, 5) == 5

block:
proc ok(_: int, _: float, a: int): int = a
doAssert ok(1, 2.0, 5) == 5

block:
proc ok(_: int, _: float, _: string, a: int): int = a
doAssert ok(1, 2.6, "5", 5) == 5

block:
iterator fn(_, _: int, c: int): int = yield c
doAssert toSeq(fn(1,2,3)) == @[3]

block:
template ok(_, _, a: int): int = a
doAssert ok(4, 2, 5) == 5

block:
doAssert not (compiles do:
template bad(_: int): int = _
discard bad(3))

block:
template ok(_: int, _: int, a: int): int = a
doAssert ok(4, 2, 5) == 5

block:
template ok(_: int, _: float, a: int): int = a
doAssert ok(1, 2.0, 5) == 5

block:
template ok(_: int, _: float, _: string, a: int): int = a
doAssert ok(1, 2.6, "5", 5) == 5

proc main() =
var x = 0

block:
proc foo(_, _: int) = x += 5

foo(1, 2)
doAssert x == 5

block:
proc foo(_: int, _: float) = x += 5

foo(1, 2)
doAssert x == 10

block:
proc foo(_: int, _: float, _: string) = x += 5

foo(1, 2, "5")
doAssert x == 15


when not defined(js):
static: main()
main()

static: test()
test()

0 comments on commit 6fce8b5

Please sign in to comment.