delegate vs function

spir denis.spir at gmail.com
Tue Nov 23 04:15:46 PST 2010


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. 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?


Denis
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com



More information about the Digitalmars-d-learn mailing list