Skip to content

Instantly share code, notes, and snippets.

@singularperturbation
Created October 30, 2016 23:37
Show Gist options
  • Save singularperturbation/3beddef7fca799dc7ea6107eb04f401c to your computer and use it in GitHub Desktop.
Save singularperturbation/3beddef7fca799dc7ea6107eb04f401c to your computer and use it in GitHub Desktop.
setControlCHook with lambda referencing local variables
import asyncdispatch
# Compiles if declared as global here:
#
# var toChange = false
proc main() =
var toChange = false
# This does not work (but does with {.noconv.} if taking out reference to
# `toChange` or if `toChange` is a global.
#
# test.nim(9, 18) Error: type mismatch: got (hook: proc (){.closure, gcsafe, locks: 0.})
# but expected one of:
# proc setControlCHook(hook: proc () not nil)
setControlCHook(hook =
proc() {.noconv.} =
toChange = true
echo "Quitting: " & $toChange
quit(0)
)
runForever()
when isMainModule:
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment