Any way to reproduce Dart style constructors?

Moritz Maxeiner via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 25 11:14:47 PDT 2017


On Thursday, 25 May 2017 at 13:53:01 UTC, ag0aep6g wrote:
> On 05/25/2017 03:13 PM, Moritz Maxeiner wrote:
>> After thinking about this a bit I think I know why it doesn't 
>> work without static and it's not a compiler bug. Since
>> 
>> ---
>> string AutoConstructor(fields ...)() {}
>> ---
>> 
>> is just syntax sugar for
>> ---
>> template AutoConstructor(fields ...)
>> {
>>      string AutoConstructor() {}
>> }
>> ---
>> 
>> instantiating the template AutoConstructor inside class Person 
>> gives you a non-static member function AutoConstructor of 
>> class Person, so obviously we need an instance of Person to 
>> call it on. Or make it a static member function.
>
> I don't think that's it.
>
> The function itself is not mixed into the class. It's called 
> and the result is mixed in. I don't see how it makes sense if 
> the compiler tries to turn the called function into a method.

Well, then I guess we need a compiler guy to clear this up, 
because from my point of view, the template is instantiated 
within the scope of the class (way before we reach the mixin), 
nesting the template's scope within the class' scope, which makes 
the function within that template's scope a member function of 
the class.


More information about the Digitalmars-d-learn mailing list