Thread overview
[phobos] static opCall for construction
May 30, 2010
Masahiro Nakagawa
May 30, 2010
Brad Roberts
May 30, 2010
Masahiro Nakagawa
May 30, 2010
Adam Ruppe
May 30, 2010
Philippe Sigaud
May 30, 2010
Denis
May 30, 2010
I noticed that some structs use static opCall for construction(lines,
CodePoints, etc...).
I think constructing struct should use this().
What are the merits of static opCall?
I will rewrite if static opCall doesn't have merits.


Masahiro
May 29, 2010
I think static opCall is there only as a workaround during the days where struct constructors were missing. It's a good idea to replace it throughout.

Andrei

On 05/29/2010 11:40 PM, Masahiro Nakagawa wrote:
> I noticed that some structs use static opCall for construction(lines,
> CodePoints, etc...).
> I think constructing struct should use this().
> What are the merits of static opCall?
> I will rewrite if static opCall doesn't have merits.
>
>
> Masahiro
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos
May 29, 2010
I'm all for removal of static opCall, and possibly opCall altogether.

On 5/29/2010 9:50 PM, Andrei Alexandrescu wrote:
> I think static opCall is there only as a workaround during the days where struct constructors were missing. It's a good idea to replace it throughout.
> 
> Andrei
> 
> On 05/29/2010 11:40 PM, Masahiro Nakagawa wrote:
>> I noticed that some structs use static opCall for construction(lines,
>> CodePoints, etc...).
>> I think constructing struct should use this().
>> What are the merits of static opCall?
>> I will rewrite if static opCall doesn't have merits.
>>
>>
>> Masahiro
>> _______________________________________________
May 30, 2010
OK, I will rewrite.


On Sun, 30 May 2010 14:22:17 +0900, Brad Roberts <braddr at puremagic.com> wrote:

> I'm all for removal of static opCall, and possibly opCall altogether.
>
> On 5/29/2010 9:50 PM, Andrei Alexandrescu wrote:
>> I think static opCall is there only as a workaround during the days where struct constructors were missing. It's a good idea to replace it throughout.
>>
>> Andrei
>>
>> On 05/29/2010 11:40 PM, Masahiro Nakagawa wrote:
>>> I noticed that some structs use static opCall for construction(lines,
>>> CodePoints, etc...).
>>> I think constructing struct should use this().
>>> What are the merits of static opCall?
>>> I will rewrite if static opCall doesn't have merits.
>>>
>>>
>>> Masahiro
>>> _______________________________________________
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos

May 30, 2010
IIRC, there is currently no way to have default ctor with non-default behavior, but it's possible to achieve with static opCall. E.g.

struct Foo
{
    this() { writeln("ctor"); } // disallowed
    static Foo opCall() { writeln("static opCall"); return Foo(); } // allowed
}

On Sun, May 30, 2010 at 8:50 AM, Andrei Alexandrescu <andrei at erdani.com> wrote:
> I think static opCall is there only as a workaround during the days where struct constructors were missing. It's a good idea to replace it throughout.
>
> Andrei
>
> On 05/29/2010 11:40 PM, Masahiro Nakagawa wrote:
>>
>> I noticed that some structs use static opCall for construction(lines,
>> CodePoints, etc...).
>> I think constructing struct should use this().
>> What are the merits of static opCall?
>> I will rewrite if static opCall doesn't have merits.
>>
>>
>> Masahiro
>> _______________________________________________
>> phobos mailing list
>> phobos at puremagic.com
>> http://lists.puremagic.com/mailman/listinfo/phobos
>
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos
>
May 30, 2010
On 5/30/10, Brad Roberts <braddr at puremagic.com> wrote:
> I'm all for removal of static opCall, and possibly opCall altogether.

Removing static opCall is definitely a good plan, but I'd like to keep opCall. The reason is so you can make a Variant that holds a delegate, and is callable directly.

I tried implementing this a while ago, but it didn't work very well because sometimes the compiler would call the static opCall instead of the one I wanted! Keeping it around, but only for the one task, would be nice to me.
May 30, 2010
Good point. The problem is that that confuses more than it helps - it's still not called for default-constructed objects.

Andrei

On 05/30/2010 04:28 AM, Denis wrote:
> IIRC, there is currently no way to have default ctor with non-default behavior, but it's possible to achieve with static opCall. E.g.
>
> struct Foo
> {
>      this() { writeln("ctor"); } // disallowed
>      static Foo opCall() { writeln("static opCall"); return Foo(); } // allowed
> }
>
> On Sun, May 30, 2010 at 8:50 AM, Andrei Alexandrescu<andrei at erdani.com>  wrote:
>> I think static opCall is there only as a workaround during the days where struct constructors were missing. It's a good idea to replace it throughout.
>>
>> Andrei
>>
>> On 05/29/2010 11:40 PM, Masahiro Nakagawa wrote:
>>>
>>> I noticed that some structs use static opCall for construction(lines,
>>> CodePoints, etc...).
>>> I think constructing struct should use this().
>>> What are the merits of static opCall?
>>> I will rewrite if static opCall doesn't have merits.
>>>
>>>
>>> Masahiro
>>> _______________________________________________
>>> phobos mailing list
>>> phobos at puremagic.com
>>> http://lists.puremagic.com/mailman/listinfo/phobos
>>
>> _______________________________________________
>> phobos mailing list
>> phobos at puremagic.com
>> http://lists.puremagic.com/mailman/listinfo/phobos
>>
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos
May 30, 2010
On Sun, May 30, 2010 at 15:38, Adam Ruppe <destructionator at gmail.com> wrote:

> On 5/30/10, Brad Roberts <braddr at puremagic.com> wrote:
> > I'm all for removal of static opCall, and possibly opCall altogether.
>
> Removing static opCall is definitely a good plan, but I'd like to keep opCall. The reason is so you can make a Variant that holds a delegate, and is callable directly.
>


Even more generally, anytime you want a callable struct (well, doh!), to
manage some complex state.
Sometimes, closures don't cut it.

For example, I like that in Clojure all data structures are functions of
their elements, returning true if the element is already present. Handy to
filter. In D, you can wrap a struct around an associative array and define
opCall(E elem) to test if (elem in aa).
Then, you can do:

auto a = filter!( [test for some elements]) (range);



> I tried implementing this a while ago, but it didn't work very well because sometimes the compiler would call the static opCall instead of the one I wanted! Keeping it around, but only for the one task, would be nice to me.
>

I also had troubles with struct with an opCall and a this and couldn't
explain to the compiler what I wanted. I had to use an .initialize() method
to create the struct, to keep () for opCall.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/phobos/attachments/20100530/ecd82af8/attachment.html>