UFCS and constructors

Kenji Hara k.hara.pg at gmail.com
Tue Jul 2 06:25:05 PDT 2013


2013/7/2 deadalnix <deadalnix at gmail.com>

> On Tuesday, 2 July 2013 at 08:16:38 UTC, Jonathan M Davis wrote:
>
>> On Tuesday, July 02, 2013 09:35:38 monarch_dodra wrote:
>>
>>> Coming back from learn here. There was an example where somebody
>>> "accidentally" called a constructor via UFCS. I am kind of
>>> surprised that it worked. I thought UFCS was for functions only,
>>> and that constructors (specifically) were off limits.
>>>
>>> Am I mistaken? Is UFCS explicitly allowed for constructors? Or
>>> did we kind of forget to take it into account?
>>>
>>
>> I'm not sure that it was ever decided one way or the other so much as
>> happened
>> into being due to how UFCS was implemented. I know that it's come up
>> before,
>> and folks were arguing on both sides. Personally, I think that it's a
>> horrible
>> idea.
>>
>> - Jonathan M Davis
>>
>
> We are 2. that is horrible.
>

I don't know what design decision had been there about it.

Historically, there's no restriction against UFCS-callable entity.
With 2.030 (released on May 11, 2009) and git head, following code
completely works.

void foo(int[]) {}
void bar(T)(T) {}

struct Foo { int[] x; }
struct Bar { this(int[]) {} }
struct Baz { static opCall(int[]) { return 0; } }

int[] function(int[]) fp;
int[] delegate(int[]) dg;

struct Functor { int opCall(int[]) { return 0; } }
Functor fn;

void main()
{
    fp = function(int[] x){ return x; };
    dg = delegate(int[] x){ return x; };

    int[] a;
    a.foo();
    a.bar();
    auto x1 = a.Foo();
    auto x2 = a.Bar();
    auto x3 = a.Baz();
    a.fp();
    a.dg();
    a.fn();
}

While improvement of dmd front-end code, I didn't touch it.
Yes, I did never designed it...

Kenji Hara
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20130702/1d57bc7e/attachment-0001.html>


More information about the Digitalmars-d mailing list