KeyType, ValueType traits for hashes

Jonathan M Davis jmdavisProg at gmx.com
Tue Jan 24 17:37:45 PST 2012


On Wednesday, January 25, 2012 01:24:59 Andrej Mitrovic wrote:
> Unfortunately you would have to do that with every template in order
> to be consistent, and that's too much work. The real improvement would
> be for the compiler to let us know *which* of the constraints failed,
> e.g.:
> 
> alias KeyType!(int[]) Key;
> 
> Error: Can't instantiate KeyType!(int) due to failed constraint: if
> (isAssociativeArray!(AA))
> 
> I don't know if there's an actual enhancement request for this but I
> think it was probably discussed before.

It's been discussed. I believe that question is how it would be implemented. 
At present, the template constraint is basically just a condition that's true 
or false. For it to pass, which pieces of it are true or false doesn't matter 
as long as the whole is true. And it becomes far more complicated when there 
are multiple template constraints, any one of them which could be true - 
though the likely solution to that is just to do what the compiler currently 
does are report failure on the first one.

I believe that Andrei is among those who want a feature along those lines, so 
there's a decent chance that we'll see it at some point, but given the more 
critical stuff that needs to be completed (e.g. TDPL compliance), I doubt that 
it's a high priority at the moment.

Regardless, the correct thing to do in most cases is to use template 
constraints, not static ifs. Using static ifs is far less flexible (and, as 
Bearophile points out, worse with error reporting as far as file and line 
number go). The solution in this case is to improve the compiler. The template 
constraint is often enough to tell what's wrong by itself anyway (and it 
definitely is in this case). Regardless, the current situation is vastly better 
than that of C++.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list