template parameter inference and introspection

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Feb 24 06:14:01 PST 2017


On Friday, 24 February 2017 at 14:06:22 UTC, Meta wrote:
> On Friday, 24 February 2017 at 11:17:46 UTC, John Colvin wrote:
>> Unfortunately that only works by accident of my example. A 
>> counterexample:
>>
>> T foo(Q = float, T = short)(T t) { return t; }
>>
>> alias Typeof(alias v) = typeof(v);
>>
>> template getInstantiation(alias f, T...)
>> {
>>     import std.meta;
>> 	
>>     alias getInstantiation = f!(staticMap!(Typeof, T));
>> }
>>
>> static assert(is(typeof(foo(3)) == int)); // ok
>> static assert(is(typeof(getInstantiation!(foo, 3)(3)) == 
>> int)); // fails
>
> This looks like VRP is kicking in when it shouldn't, or maybe 
> it's a different bug. 3 should be typed as int by default 
> unless we explicitly ask for something else.

VRP propagation is what makes the call possible, but that's a 
distraction. The problem is that getInstantiation is setting Q to 
int and leaving T to be it's default type of short, which is not 
the same as if you just call with an int (which infers T from t 
and leaves Q as it's default float. Fundamentally, you can't 
assume the same order of runtime and template arguments.


More information about the Digitalmars-d-learn mailing list