www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Default Delegate Parameter

reply Jesse Phillips <jessekphillips+D gmail.com> writes:
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();
}
Jan 26 2010
parent reply BCS <none anon.com> writes:
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(); } -- <IXOYE><
Jan 26 2010
parent Jesse Phillips <jessekphillips+D gmail.com> writes:
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.
Jan 26 2010