lambdas and function literals in classes

Dicebot public at dicebot.lv
Tue Jul 9 03:59:15 PDT 2013


On Tuesday, 9 July 2013 at 10:50:02 UTC, John Colvin wrote:
> JS asked about this in the main group, but here is more 
> appropriate and I'm quite interested myself.
>
> Can someone explain the rationale behind this:
>
> class A
> {
>     auto a = (){}; 		//Lambda not allowed
>     auto b = function(){};	//Function allowed
>     auto c = delegate(){};	//Delegate not allowed
> }
>
> A guess:
>
> Delegate's aren't allowed as members due their context pointer 
> (why???), lambdas are assumed to be delegates unless they are 
> proved to not need context (and dmd is sucking at proving that).

Error message is actually misleading here, because this compiles:

class A
{
     void delegate() a;
}

void main()
{
	auto a = new A();
	a.a = () {};
}

So this has something to do with _initialization_ of class 
members with delegates/lambdas, not their very existence.


More information about the Digitalmars-d-learn mailing list