Any way to reproduce Dart style constructors?
Moritz Maxeiner via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu May 25 03:52:57 PDT 2017
On Thursday, 25 May 2017 at 10:42:00 UTC, ag0aep6g wrote:
> On 05/25/2017 10:34 AM, JN wrote:
>> class Person
>> {
>> string name;
>> int age;
>> mixin(AutoConstructor!(age, name));
>> }
> [...]
>> I am not looking for code, I can try that myself, just asking
>> if such things are possible?
>
> I know you're not asking for code, but without experimenting I
> wouldn't have known that this works. In the end it's
> surprisingly simple:
>
> ----
> mixin template AutoConstructor(fields ...)
> {
> this(typeof(fields) args) { fields = args; }
> }
> ----
Be aware, though, that constructors mixed in via a mixin template
behave differently with regards to overloading[1].
[1] https://issues.dlang.org/show_bug.cgi?id=11500
More information about the Digitalmars-d-learn
mailing list