Any way to reproduce Dart style constructors?

ag0aep6g via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 25 06:53:01 PDT 2017


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.


More information about the Digitalmars-d-learn mailing list