July 10, 2012 Return type inference on template | ||||
---|---|---|---|---|
| ||||
Attachments:
| Simple template: T test(T)() { return T.init; } This code give error: int myVar; myVar = test(); Why? Can't compiler guess T == int? |
July 10, 2012 Re: Return type inference on template | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | 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.
|
Copyright © 1999-2021 by the D Language Foundation