Request for a more powerful template specialization feature

data pulverizer via Digitalmars-d digitalmars-d at puremagic.com
Fri Jul 14 11:19:03 PDT 2017


Dear all,

Template specializations are a great feature in D. They allow the 
programmer to create template specializations but they can also 
be a powerful way of constraining templates by implementing only 
the specializations that you need. In contrast template 
constraints can quickly become very complex for the programmer to 
write and reason about.

Template specializations should be extended to allow multiple 
lists of types to be implemented together. For example this ...

template Construct(R: Union{double, int}, W: Union{string, char, 
dchar})
{
     auto Construct(R, W)(R r, W w)
     {
         ....
     }
}

The same definition would be allowed for all 6 combinations of 
{double, int} and {string, char, dchar} (and no more! Unless 
specified otherwise and/or more generally). This would remove the 
need to manually write these for all combinations or resort to 
constraints.

In addition for some use cases it is a nicer way of doing unions 
than using the current union keyword. Union{double, int} could be 
a compile-time construct indicating that the type can be either 
an actual double or int. It can replace the use of union in cases 
where you want a substitution of either double or int for this 
Union rather than a union type. In addition, variants return 
variants rather than "properly" typed data.

It would be good to get comments and suggestions before I write a 
DIP.

Thank you in advance.



More information about the Digitalmars-d mailing list