Default Delegate Parameter

Jesse Phillips jessekphillips+D at gmail.com
Tue Jan 26 17:57:43 PST 2010


BCS wrote:

> Hello Jesse,
>
>> For the following code I get the bellow error. I'm wondering if I
>> should be reporting a bug, or if creating default delegates is
>> correctly prevented?
>> 
>> .\defltdg.d(10): Error: delegate defltdg.__dgliteral3 is a nested
>> function and cannot be accessed from main
>> 
>> import std.stdio;
>> 
>> void main() {
>> take(() {writeln("Hello world");});
>> take(() {});
>> take();
>> }
>> void take(void delegate() dg = () {}) {
>> dg();
>> }
>
> I don't know if this is a bug or what but I think this happens because the 
> default is defined (compile time) in the scope of take but generated (run 
> time) in the scope of main. 
>
> I'd be fine with a special case for this that either 1) allows a delegate 
> that doesn't access any outer scope to be generated like that or 2) special 
> case the code gen to correctly generate the delegate for the function (the 
> new frame pointer can be computed at that point)
>
> work around:
>
> void take() { take(() {});}
> void take(void delegate() dg) { dg(); }


At least that explains why, should have mentioned I had the work around.

Thanks.



More information about the Digitalmars-d-learn mailing list