On Tue, 23 Nov 2010 13:15:46 +0100, spir wrote: > Hello, > > alias void function (int) F; > alias void delegate (int) D; > > void fnFunc (F f, int i) {f(i);} > void dgFunc (D d, int i) {d(i);} > > void writeOut (int i) {writeln(i);} > > void test () { > void writeIn (int i) {writeln(i);} > fnFunc(&writeOut, 1); > dgFunc(&writeIn, 1); > //~ fnFunc(&writeIn, 1); // error (expected a func, got a > delegate...) //~ dgFunc(&writeOut, 1); // error (... and > conversely) } > > If a function is defined at the module's toplevel and then passed (via a > pointer) to a higher-order func that expects a function, al works fine. > But if it is defined inside a function, then the pointer is > automatically typed as delegate, even if the function does not use any > variable in scope, and I get an error.
Mark the function as 'static', like this: static void writeIn(int i) { ... } Then the compiler even ensures that it doesn't use any symbols from the enclosing scope. > Conversely, if the higher order > func is defined to expect a delegate, then it fails if I pass a func > defined at the top-level. How to solve this? Use std.functional.toDelegate(), like this: dgFunc(toDelegate(&writeOut), 1); (For some reason the documentation for toDelegate() seems to be missing from the D web site, but I don't know why. I'll look into it.) -Lars