Any way to reproduce Dart style constructors?

JN via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 25 01:34:54 PDT 2017


One of my favourite language features of Dart (other one being 
factory constructors) are auto-assign constructors, for example 
(writing it in pseudo-D):

class Person
{
   string name;
   int age;
   this(this.age, this.name);
}

would translate to

class Person
{
   string name;
   int age;
   this(int age, string name)
   {
     this.age = age;
     this.name = name;
   }
}


It saves a lot of typing in the long run when doing lots of OOP, 
is there a way to reproduce such behaviour using mixins? I was 
thinking of some interface like:

class Person
{
   string name;
   int age;
   mixin(AutoConstructor!(age, name));
}

but I don't know if that's even doable using mixins. Even cooler 
might be something like an annotation (feels a bit Lombok-like 
from Java):

@AutoConstructor
class Person
{
   string name;
   int age;
}

but I don't think it's doable in D right now.

I am not looking for code, I can try that myself, just asking if 
such things are possible?


More information about the Digitalmars-d-learn mailing list