Named constructors

evilrat evilrat666 at gmail.com
Thu Jan 10 13:59:36 UTC 2019


On Thursday, 10 January 2019 at 13:44:52 UTC, Dru wrote:
>> Here is the one without templates
>>
>>
>> struct Stuff_Normal {}
>> struct Stuff_Special {}
>>
>> struct Stuff
>> {
>>
>>   this(Stuff_Normal _, float x, float y) { ... }
>>   this(Stuff_Special _, float x, float y) { ... }
>> }
>>
>> usage
>>
>>   auto val = Stuff(Stuff_Normal(), 1, 2);
>
> Not pretty syntax but I think this is the most practical 
> solution :)

well, you can also make factory function or static factory method 
that makes tags or just alias it to enum/constant to get rid of 
ctor call, also since tag parameter is unused name can be omitted 
(though this will make it look magical for someone unfamiliar 
with this use case)


More information about the Digitalmars-d mailing list