Given the following examplar:
import std/options
type
Callback = Option[proc(name: string, length: int)]
TestOptions = object
name: string
length: int
callback: Callback
proc show(name: string, length: int) =
echo "Callback executed: ", name, " is ", length, " inches long"
let option1 = TestOptions(name: "foot", length: 12)
let option2 = TestOptions(name: "inch", length: 1)
#let option3 = TestOptions(name: "yard", length: 36, callback: some(show))
# FIXME: uncomment
proc test(options: TestOptions) =
echo "Options = ", options
if options.callback.isSome:
options.callback.get()(options.name, options.length)
else:
echo "No callback supplied"
test option1
test option2
#test option3 # FIXME: uncomment
Run
Running the code gives:
nim r tests/test_options.nim
Options = (name: "foot", length: 12, callback: ...)
No callback supplied
Options = (name: "inch", length: 1, callback: ...)
No callback supplied
Run
If I uncomment the two **FIXME** lines to test actually supplying a callback, I
get the following compile error:
nim r tests/test_options.nim
/Users/DM/work/trunk/nim/tests/test_options.nim(16, 62)
Error: type mismatch: got 'Option[proc (name: string, length:
int){.gcsafe.}]' for 'some(show)' but
expected 'Callback = Option[proc (name: string, length: int){.closure.}]'
Run
Anyone know the _magic incantation_ to allow the code to compile and hopefully
run? I tried sprinkling {.closure.} and {.gcsafe.} pragmas around with no
success.