February 22, 2012
Le 22/02/2012 03:59, Vladimir Panteleev a écrit :
> 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");
> };
> }

This look very promizing !!!

I'll investigate in that direction. Thank you very much for the hint.
February 22, 2012
Le 21/02/2012 18:46, Jacob Carlborg a écrit :
> On 2012-02-21 16:55, deadalnix wrote:
>> Le 21/02/2012 16:48, Adam D. Ruppe a écrit :
>>> A possible workaround is to initialize the delegate
>>> in the object's constructor.
>>
>> It is a struct. And struct don't have default constructor. It lead to
>> very segfault prone code (I did try that).
>
> You can implement a static opCall and use that instead of the constructor.
>

It's a cheap replacement because it doesn't allow to new.
February 22, 2012
On 02/21/2012 04:24 PM, 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 ?

Bug or not, It is an annoying limitation.
February 22, 2012
On Wed, Feb 22, 2012 at 12:42:44PM +0100, deadalnix wrote:
> Le 22/02/2012 03:59, Vladimir Panteleev a écrit :
[...]
> >struct stuff {
> >private Exception function() exceptionBuilder =
> >&defaultExceptionBuilder;
> >
> >private static Exception defaultExceptionBuilder() {
> >return new Exception("foobar");
> >};
> >}
> 
> This look very promizing !!!
> 
> I'll investigate in that direction. Thank you very much for the hint.

But doesn't this only work for function pointers? I don't think you can later assign a *delegate* to exceptionBuilder. They are different types (delegates need fat pointers, won't fit in a function pointer).


T

-- 
Trying to define yourself is like trying to bite your own teeth. -- Alan Watts
February 22, 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

February 25, 2012
On 21/02/2012 17:46, Jacob Carlborg wrote:
> On 2012-02-21 16:55, deadalnix wrote:
<snip>
> You can implement a static opCall and use that instead of the constructor.

But you don't have to call a static opCall.  You can just declare a struct instance without any initialisation.  Presumably half the point is to ensure that exceptionBuilder is still initialised in such cases.

Stewart.
February 25, 2012
On 02/25/2012 03:32 PM, Stewart Gordon wrote:
> On 21/02/2012 17:46, Jacob Carlborg wrote:
>> On 2012-02-21 16:55, deadalnix wrote:
> <snip>
>> You can implement a static opCall and use that instead of the
>> constructor.
>
> But you don't have to call a static opCall. You can just declare a
> struct instance without any initialisation. Presumably half the point is
> to ensure that exceptionBuilder is still initialised in such cases.
>
> Stewart.

struct exceptionBuilder{
    @disable this();
    @disable enum init=0;
    static exceptionBuilder opCall(...){...}
}
February 25, 2012
On 02/25/12 15:37, Timon Gehr wrote:
> On 02/25/2012 03:32 PM, Stewart Gordon wrote:
>> On 21/02/2012 17:46, Jacob Carlborg wrote:
>>> On 2012-02-21 16:55, deadalnix wrote:
>> <snip>
>>> You can implement a static opCall and use that instead of the constructor.
>>
>> But you don't have to call a static opCall. You can just declare a struct instance without any initialisation. Presumably half the point is to ensure that exceptionBuilder is still initialised in such cases.
>>
>> Stewart.
> 
> struct exceptionBuilder{
>     @disable this();
>     @disable enum init=0;
>     static exceptionBuilder opCall(...){...}
> }

Too bad this doesn't work when opCall takes args.

IOW, "S(2)" results in

 Error: constructor m.S.this () is not callable using argument types (int)
 Error: constructor m.S.this is not callable because it is annotated with @disable
 Error: expected 0 arguments, not 1 for non-variadic function type ref S()

Removing the disabled 'this()' makes the error go away and the right
opCall() gets called.

artur
February 25, 2012
On 02/25/2012 07:23 PM, Artur Skawina wrote:
> On 02/25/12 15:37, Timon Gehr wrote:
>> On 02/25/2012 03:32 PM, Stewart Gordon wrote:
>>> On 21/02/2012 17:46, Jacob Carlborg wrote:
>>>> On 2012-02-21 16:55, deadalnix wrote:
>>> <snip>
>>>> You can implement a static opCall and use that instead of the
>>>> constructor.
>>>
>>> But you don't have to call a static opCall. You can just declare a
>>> struct instance without any initialisation. Presumably half the point is
>>> to ensure that exceptionBuilder is still initialised in such cases.
>>>
>>> Stewart.
>>
>> struct exceptionBuilder{
>>      @disable this();
>>      @disable enum init=0;
>>      static exceptionBuilder opCall(...){...}
>> }
>
> Too bad this doesn't work when opCall takes args.
>
> IOW, "S(2)" results in
>
>   Error: constructor m.S.this () is not callable using argument types (int)
>   Error: constructor m.S.this is not callable because it is annotated with @disable
>   Error: expected 0 arguments, not 1 for non-variadic function type ref S()
>
> Removing the disabled 'this()' makes the error go away and the right
> opCall() gets called.
>
> artur

Well, that is a bug.

http://d.puremagic.com/issues/show_bug.cgi?id=6036
1 2
Next ›   Last »