Skip to content

Commit

Permalink
workaround for #12020 (caused by new gensym handling for params)
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Sep 8, 2019
1 parent 0023347 commit 21c9ad3
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/pure/lambdas.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,22 @@ template lambdaIt*(a: untyped): untyped =

macro `~>`*(lhs, rhs: untyped): untyped =
#[
TODO: allow param constraints, eg: (a, b: int, c) ~> a*b+c
side effect safe (ie, arguments will be evaluated just once, unlike templates)
note: side effect safe (ie, arguments will be evaluated just once, unlike templates)
could also allow param constraints, eg: (a, b: int, c) ~> a*b+c
]#
let name = genSym(nskTemplate, "lambdaArrow")
let formatParams2 = nnkFormalParams.newTree()
formatParams2.add ident("untyped")
var body2 = newStmtList()

template addArg(argInject) =
let arg = genSym(nskParam, argInject.strVal)
# this doesn't work since new gensym, see #12020
# let arg = genSym(nskParam, argInject.strVal)
# so using this workaround instead:
var count {.threadvar.}: int
count.inc
let arg = newIdentNode(argInject.strVal & "_fakegensym_" & $count)

formatParams2.add newTree(nnkIdentDefs, arg, ident("untyped"), newEmptyNode())
# CHECKME: let or var?, eg var could be needed?
body2.add newLetStmt(argInject, arg)
Expand All @@ -52,7 +58,6 @@ macro `~>`*(lhs, rhs: untyped): untyped =
error("expected " & ${nnkPar,nnkIdent} & " got `" & $kind & "`")

body2.add rhs

body2 = quote do: # TODO: option whether to use a block?
block: `body2`

Expand Down

0 comments on commit 21c9ad3

Please sign in to comment.