delegate as memeber

Mantis mail.mantis.88 at gmail.com
Tue Feb 21 08:30:43 PST 2012


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;
}


More information about the Digitalmars-d-learn mailing list