Suggestion: final struct initializers
Q. Schroll
qs.il.paperinik at gmail.com
Thu Dec 10 00:18:28 UTC 2020
On Friday, 4 December 2020 at 16:15:33 UTC, Mr T. wrote:
> On Friday, 4 December 2020 at 15:25:13 UTC, Jacob Carlborg
> wrote:
>>
>> You can just use a constructor to achieve the same thing.
>
> Constructors need writing, these things exist automatically.
True, but if you really need it multiple times, a mixin template
can serve you well: A quick and easy one is
mixin template FinalCtor()
{
this(typeof(this.tupleof) args)
{
this.tupleof[] = args[];
}
}
but error messages if you forget arguments are rather bad. You
could (in increasing value but also increasing difficulty)
(1) Add a templated @disable'd overload that catches all others
and generates a @disabled error message.
(2) Add @disable'd overload that are one or more arguments short.
(3) Use https://dlang.org/phobos/std_traits.html#FieldNameTuple
and other reflection to actually insert the member names. That
way, you can use named arguments as soon as the corresponding DIP
is implemented and best error messages. This solution is very
convoluted using string mixins and from a maintenance viewpoint
is worse than (2).
I've done (2) in https://run.dlang.io/is/vAGRMV
and (3) in https://run.dlang.io/is/PMT1PT
More information about the Digitalmars-d
mailing list