enums and std.traits

Jonathan M Davis jmdavisProg at gmx.com
Sun Aug 5 13:55:36 PDT 2012


On Sunday, August 05, 2012 12:38:56 Christophe Travert wrote:
> Jonathan M Davis , dans le message (digitalmars.D:174267), a écrit :
> > On Saturday, August 04, 2012 15:22:34 Jonathan M Davis wrote:
> >> On Sunday, August 05, 2012 00:15:02 Timon Gehr wrote:
> >> > T fun(T)(T arg) if(isSomeString!arg){
> >> > 
> >> >      return arg~arg[0];
> >> > 
> >> > }
> 
> IMO, the behavior should be this: when trying to call the template with
> a argument that is an enum type based on string, the compiler should try
> to instanciate the template for this enum type, and isSomeString should
> fail. Then, the the compiler will try to instanciate the template for
> strings, which works. Thus, the template is called with a string
> argument, which is the enum converted to a string.

I don't believe that the compiler ever tries twice to instantiate _any_ 
template. It has a specific type that it uses to instantiate the template 
(usually the exact type passed or the exact type of the value passed in the 
case of IFTI - but in the case of IFTI and arrays, it's the tail const version 
of the type that's used rather than the exact type). If it works, then the 
template is instantiated. If it fails, then it doesn't. There are no second 
tries with variations on the type which it could be implicitly converted to.

And honestly, I think that doing that would just make it harder to figure out 
what's going on when things go wrong with template instantiations. It would be 
like how C++ will do up to 3 implicit conversions when a function is called, 
so you don't necessarily know what type is actually being passed to the 
function ultimately. It can be useful at times, but it can also be very 
annoying. D explicitly did not adopt that sort of behavior, and trying 
multiple types when instantiating a template would not be in line with that 
decision.

- Jonathan M Davis


More information about the Digitalmars-d mailing list