delegate as memeber

deadalnix deadalnix at gmail.com
Tue Feb 21 11:01:18 PST 2012


Le 21/02/2012 17:30, Mantis a écrit :
> 21.02.2012 17:24, deadalnix пишет:
>> struct stuff {
>> private Exception delegate() exceptionBuilder = delegate Exception() {
>> return new Exception("foobar");
>> };
>> }
>>
>> The following piece of code trigger a compiler error : delegate
>> module.stuff.__dgliteral1 function literals cannot be class members
>>
>> Why is that ? Is it a bug or a feature ?
>>
>
> The compiler expects member initializers to be known at compile-time.
> Since delegate carries closure, and closure is a run-time phenomena, you
> cannot put it there. That's how I understand it, and I might be wrong.
> Anyway, something like this is possible as a workaround:
>
> struct Foo {
> private Exception dg() {
> if( m_Dg ) return m_Dg();
> return new Exception( "foobar" );
> }
>
> private Exception delegate() m_Dg = null;
> }

I think this the best solution after all.

But still I think the original code should be an error only if it use 
data out of the delegate scope. If it doesn't, frame pointer doesn't 
matter and null can be passed.


More information about the Digitalmars-d-learn mailing list