-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added doc + runnableExamples for lambdaEval
- Loading branch information
1 parent
48d3390
commit 29db6fb
Showing
1 changed file
with
13 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,22 @@ | ||
import macros | ||
|
||
macro lambdaEval*(lambda: untyped, arg:typed):untyped= | ||
## allows using zero-cost lambda expressions ``a=>b`` in templates. Type | ||
## inference is done at evaluation site, so user doesn't need to specify | ||
## types in the lambda expression. | ||
runnableExamples: | ||
template testCallFun[T](fun: untyped, a:T): auto = | ||
lambdaEval(fun, lambdaEval(fun, a)) | ||
doAssert testCallFun(x => x * 3, 2) == (2 * 3) * 3 | ||
|
||
if $lambda[0] != "=>": | ||
error("Expected `=>` got " & $lambda[0]) | ||
var ret = newStmtList() | ||
ret.add newLetStmt(lambda[1], arg) | ||
ret.add lambda[2] | ||
result = newBlockStmt(ret) | ||
|
||
when isMainModule: | ||
block lambdaEvalTest: | ||
# PENDING https://github.com/nim-lang/Nim/issues/7280 | ||
discard lambdaEval(a => a, 0) |