Is there a nice syntax to achieve optional named parameters?

John Burton john.burton at jbmail.com
Wed Jan 16 13:31:18 UTC 2019


On Wednesday, 16 January 2019 at 11:21:53 UTC, Dukc wrote:
> On Tuesday, 15 January 2019 at 11:14:54 UTC, John Burton wrote:
>> This is ok, but I'm not so keen on separating the creation and 
>> construction like this.
>> Is there a better way that's not ugly?
>
> You can make the constructor a template that takes a single 
> struct of arbitrary, and inspects (at compile time) if it has 
> fields with certain names and types. Then, when constructing, 
> you feed that constructor a std.typecons.Tuple with named 
> fields. Or alternatively, use a separate builder type that 
> makes a good struct to feed for the window constructor.
>
> The disadvantage is that you cannot link the constructor 
> template directly for external programs. But for that, you 
> define some sort of wrapper function that always takes all the 
> parameters and then calls the template.

Thanks, I tried out the tuple approach and it works very well.
Constructing a tuple at the point of call with named fields works 
well, but looks a bit "ugly" to me but I might use it.
I think on balance that creating a separate builder struct that I 
can set the fields in and pass to the "real" constructor might be 
the way to go though for me.


More information about the Digitalmars-d-learn mailing list