delegate as memeber
Ali Çehreli
acehreli at yahoo.com
Wed Feb 22 13:58:25 PST 2012
On 02/21/2012 07:43 AM, 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.
Could std.functional.toDelegate be helpful here?
http://dlang.org/phobos/std_functional.html#toDelegate
Ali
More information about the Digitalmars-d-learn
mailing list