digitalmars.D.learn - Return type inference on template
- Andrea Fontana (6/6) Jul 10 2012 Simple template:
- Timon Gehr (10/16) Jul 10 2012 Type deduction only proceeds in the direction the data flows.
Simple template: T test(T)() { return T.init; } This code give error: int myVar; myVar =3D test(); Why? Can't compiler guess T =3D=3D int?
Jul 10 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.
Jul 10 2012