Named constructors

evilrat evilrat666 at gmail.com
Thu Jan 10 04:38:27 UTC 2019


On Wednesday, 9 January 2019 at 20:54:48 UTC, JN wrote:
> On Wednesday, 9 January 2019 at 10:02:12 UTC, bauss wrote:
>>
>> I like this approach to it and you're entirely correct.
>>
>> It would be nice with support for this in the language without 
>> having to create factory methods.
>>
>> Basically something like the following could be lowered to 
>> your code.
>
> I was going to say that this post will be mostly ignored and 
> people will present some template magic to partially implement 
> such feature (just add few imports and some boilerplate code), 
> but I guess I got beaten to it :)

Surprisingly no one was thinking about tags...

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);


But honestly Atila version with distinct types was the best IMHO, 
because you know, one doesn't want to add temperature to angle or 
length to money


More information about the Digitalmars-d mailing list