Suggestion: "new delegate" - delegates useable outside of the nesting function scope

Markus Dangl danglm at in.tum.de
Thu Jun 15 11:19:30 PDT 2006


> Hmm.. what's stopping you from returning a 'normal' delegate, thru 
> return &readerFunc(f).Eval or &readerFunc(f).opCall ? it would make more 
> sense to functions which accept char delegate() and have no idea what 
> AdvancedDelegate0!(char) is :)

The Eval() method is there to really do the evaluation of the delegate. 
You can easily "convert" an AdvancedDelegate to a delegate by 
referencing its Eval() method:


char delegate() createFileReader(char[] fileName)
{
     File f = new File(fileName, FileMode.In);

     auto readerFunc = AdvancedDelegate(
         function char(File f)
         {
             return f.getc();
         }
     );
     return &(readerFunc(f).Eval);
}

I see that this isn't very intuitive, so i'm going to add a 
"GetDelegate" method and overload the "opCast" operator - you can then 
also write:

return readerFunc(f).GetDelegate;

or:

return cast(char delegate()) (readerFunc(f));



More information about the Digitalmars-d mailing list