Hi!
What am I doing wrong here:
type
FooProc = proc(x: int)
Callbacks = tuple [
cb: FooProc
]
proc foo(x: int) : void = echo "foo: " & $x
let f:FooProc = foo
let c0: Callbacks = (cb: f) # works fine
let c1: Callbacks = (cb: foo) # type mismatch!
c0.cb(42)
c1.cb(42)
The assignment to c1 complains, saying:
: type mismatch: got (tuple[cb: proc (x: int){.gcsafe, locks: 0.}]) but expected 'Callbacks = tuple[cb: FooProc]'
But surely f and foo are the same? Shouldn't that assignment either always fail, or always be OK? What am I missing?
Thanks!
There is an automatic conversion from nimcall to closure but not in tuple constructors (for better or worse).
What are the precise conditions under which one should choose a tuple over an object? I'd use them for points in n-space for small n, and other tasks where I wrap up a very small number of builtin types and I expect a lot of them, but not much else. I almost always use objects, because tuples often surprise me.
In this case, using an object works as @luked2 expected.