delegate as memeber

Vladimir Panteleev vladimir at thecybershadow.net
Tue Feb 21 18:59:16 PST 2012


On Tuesday, 21 February 2012 at 15:41:58 UTC, deadalnix wrote:
> Le 21/02/2012 16:32, Vladimir Panteleev a écrit :
>> On Tuesday, 21 February 2012 at 15:22:15 UTC, deadalnix wrote:
>>> 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 ?
>>
>> Delegates contain a context pointer. Your delegate literal has 
>> no context.
>>
>> You can't initialize it with the address of a method, either. 
>> For struct
>> methods, the context pointer is a pointer to the structure. 
>> You can't
>> have a .init that contains a pointer to an instance. You 
>> probably want
>> to use a function literal.
>
> It doesn't work with function either.
>
> But I need delegate here. The default one doesn't require a 
> context, but isn't it possible to pass null as a context, as 
> none is needed ? This value can be changer later, and 
> definitively require to be a delegate.

struct stuff {
	private Exception function() exceptionBuilder =
		&defaultExceptionBuilder;
	
	private static Exception defaultExceptionBuilder() {
		return new Exception("foobar");
	};
}


More information about the Digitalmars-d-learn mailing list