type Foo = proc ()
proc goWith(fn: Foo) =
fn()
goWith proc() =
echo "OK!"
goWith Foo =
echo "Bad!"
This compiles failed:
d:\test\nim\nim01.nim(9, 8) Error: type mismatch: got (typedesc[Foo]) but expected one of: nim01.goWith(fn: Foo)
How can I make the later style anonymous proc work? Thanks a lot.
You need to import future http://nim-lang.org/docs/future.html#=>.m,expr,expr
import future
goWith(()=> echo "Yeah!")
The goWith Foo part is ill-formed code. You are passing the type Foo to procedure goWith, then compare this result with the (non-existent) return value of echo "Bad!". This is why you're getting an error message complaining about passing typedesc[Foo], when a procedure argument was expected.
When you write type Foo = proc(), this creates a type alias called Foo. You cannot use this type alias as an alternate syntax for procedure literals (which is what proc() = echo "OK!" is. If you want to do this, you'll need a template, e.g.:
type Foo = proc ()
template FooLit(f: untyped): Foo =
(proc() = f)
proc goWith(fn: Foo) =
fn()
goWith proc() =
echo "OK!"
goWith FooLit do:
echo "Bad!"
@Jehan
Thank you, I do understand now. Well, that means type define a proc 's usage is very limited...