diff options
author | konsumlamm <44230978+konsumlamm@users.noreply.github.com> | 2021-02-03 03:11:59 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-02 18:11:59 -0800 |
commit | f4449a897ddb094fbc2e1c96eea3d8a8afdcd76a (patch) | |
tree | 5a5629e1c3d302fcdbf4df42e59eb0f3c8957211 /lib/pure | |
parent | e4ad0ae5c41fa847797fb4d25bcabff3cdcb8966 (diff) | |
download | Nim-f4449a897ddb094fbc2e1c96eea3d8a8afdcd76a.tar.gz |
Sugar tests (#16820)
Diffstat (limited to 'lib/pure')
-rw-r--r-- | lib/pure/sugar.nim | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/lib/pure/sugar.nim b/lib/pure/sugar.nim index 5e71ca54b..5cece12b6 100644 --- a/lib/pure/sugar.nim +++ b/lib/pure/sugar.nim @@ -53,8 +53,8 @@ proc createProcType(p, b: NimNode): NimNode {.compileTime.} = result.add prag macro `=>`*(p, b: untyped): untyped = - ## Syntax sugar for anonymous procedures. - ## It also supports pragmas. + ## Syntax sugar for anonymous procedures. It also supports pragmas. + # TODO: xxx pending #13491: uncomment in runnableExamples runnableExamples: proc passTwoAndTwo(f: (int, int) -> int): int = f(2, 2) @@ -62,13 +62,16 @@ macro `=>`*(p, b: untyped): untyped = type Bot = object - call: proc (name: string): string {.noSideEffect.} + call: (string {.noSideEffect.} -> string) var myBot = Bot() myBot.call = (name: string) {.noSideEffect.} => "Hello " & name & ", I'm a bot." doAssert myBot.call("John") == "Hello John, I'm a bot." + # let f = () => (discard) # simplest proc that returns void + # f() + var params = @[ident"auto"] name = newEmptyNode() @@ -140,15 +143,20 @@ macro `=>`*(p, b: untyped): untyped = procType = kind) macro `->`*(p, b: untyped): untyped = - ## Syntax sugar for procedure types. + ## Syntax sugar for procedure types. It also supports pragmas. runnableExamples: proc passTwoAndTwo(f: (int, int) -> int): int = f(2, 2) - # is the same as: # proc passTwoAndTwo(f: proc (x, y: int): int): int = f(2, 2) doAssert passTwoAndTwo((x, y) => x + y) == 4 + proc passOne(f: (int {.noSideEffect.} -> int)): int = f(1) + # is the same as: + # proc passOne(f: proc (x: int): int {.noSideEffect.}): int = f(1) + + doAssert passOne(x {.noSideEffect.} => x + 1) == 2 + result = createProcType(p, b) macro dump*(x: untyped): untyped = @@ -216,7 +224,7 @@ macro capture*(locals: varargs[typed], body: untyped): untyped {.since: (1, 1).} ## Useful when creating a closure in a loop to capture some local loop variables ## by their current iteration values. runnableExamples: - import std/[strformat, sequtils] + import std/strformat var myClosure: () -> string for i in 5..7: @@ -226,12 +234,6 @@ macro capture*(locals: varargs[typed], body: untyped): untyped {.since: (1, 1).} myClosure = () => fmt"{i} * {j} = 42" doAssert myClosure() == "6 * 7 = 42" - let m = @[(s: string) => "to " & s, - (s: string) => "not to " & s] - let l = m.mapIt(capture(it, (s: string) => it(s))) - let r = l.mapIt(it("be")) - doAssert fmt"{r[0]}, or {r[1]}" == "to be, or not to be" - var params = @[newIdentNode("auto")] let locals = if locals.len == 1 and locals[0].kind == nnkBracket: locals[0] else: locals |