Return type inference on template

Timon Gehr timon.gehr at gmx.ch
Tue Jul 10 07:41:18 PDT 2012


On 07/10/2012 04:14 PM, Andrea Fontana wrote:
> Simple template:
>
> T test(T)() { return T.init; }
>
> This code give error:
>
> int myVar;
> myVar = test();
>
> Why? Can't compiler guess T == int?
>

Type deduction only proceeds in the direction the data flows.

Reversing this process would in principle work in a few cases, such as
the one you present here.

I think it would be neat to have more powerful IFTI. An even more
important improvement would be to allow parameters to cross-talk, eg:

auto map(R,S,T...)(R range, S delegate(ElementType!R) dg) { ... }

[1,2,3].map(x=>2*x); // currently an error, but could be made to work

Note that full type inference cannot be achieved in D, because the type
system is Turing complete.


More information about the Digitalmars-d-learn mailing list