nim-stew/tests/test_faux_closures.nim
Zahary Karadjov 32b86bfd1f
Faux closures: a facility for creating closure-like non-closure procs
This code is extracted from the error-handling proposal where it's
used to implement `Try` blocks (Faux closures are created there in
order to assign raises lists to them). The desktop team faced a
similar problem where the `spawn` API doesn't support closures,
but the restriction can be easily worked-around with faux closures.
2020-07-07 20:29:04 +03:00

23 lines
340 B
Nim

import
os, threadpool,
../stew/faux_closures
template sendSignal(data: string) =
echo data
template spawnAndSend(exprToSend: untyped) =
proc payload {.fauxClosure.} =
let data = exprToSend
sendSignal data
spawn payload()
proc main(x: string) =
spawnAndSend:
var i = 10
x & $(23 + i)
main("tests")
sleep(2)