digitalmars.D.learn - More templated type argument inference
- bearophile (15/15) Dec 06 2012 Currently this code is not supported:
- Maxim Fomin (3/19) Dec 06 2012 The second is template lambda and templates have void type. What
- bearophile (9/11) Dec 06 2012 In D template functions become functions if they are passed where
- Maxim Fomin (4/20) Dec 06 2012 I forgot there is int here, so T in lambda should be deduced as
- Philippe Sigaud (10/10) Dec 06 2012 Doesn't that mean you're asking the compiler to have a full unification
- bearophile (6/10) Dec 06 2012 I think the general case doesn't require a whole program type
- Timon Gehr (2/17) Dec 07 2012 I consider it necessary.
- bearophile (5/6) Dec 07 2012 To consider opening an enhancement request, are you able to
- Timon Gehr (24/29) Dec 07 2012 It is just embarrassing if it does not work. Similar things work even in...
- bearophile (10/13) Dec 07 2012 Even if they are trying to speed up Scala compilation, it's much
Currently this code is not supported: void foo(T)(T x, T function(T) f) {} void main() { foo(1, (int a) => a * a); // OK foo(1, a => a * a); // Error } With the latest alpha compiler it gives: test.d(4): Error: template test.foo does not match any function template declaration. Candidates are: test.d(1): test.foo(T)(T x, T function(T) f) test.d(4): Error: template test.foo(T)(T x, T function(T) f) cannot deduce template function from argument types !()(int,void) Do you think it's a good idea to allow it? Bye, bearophile
Dec 06 2012
On Thursday, 6 December 2012 at 13:04:05 UTC, bearophile wrote:Currently this code is not supported: void foo(T)(T x, T function(T) f) {} void main() { foo(1, (int a) => a * a); // OK foo(1, a => a * a); // Error } With the latest alpha compiler it gives: test.d(4): Error: template test.foo does not match any function template declaration. Candidates are: test.d(1): test.foo(T)(T x, T function(T) f) test.d(4): Error: template test.foo(T)(T x, T function(T) f) cannot deduce template function from argument types !()(int,void) Do you think it's a good idea to allow it? Bye, bearophileThe second is template lambda and templates have void type. What you suggest here?
Dec 06 2012
Maxim Fomin:The second is template lambda and templates have void type. What you suggest here?In D template functions become functions if they are passed where the D compiler can infer their full type. This is correct code: void foo(int function(int) f) {} void main() { foo(a => a * a); } Bye, bearophile
Dec 06 2012
On Thursday, 6 December 2012 at 13:04:05 UTC, bearophile wrote:Currently this code is not supported: void foo(T)(T x, T function(T) f) {} void main() { foo(1, (int a) => a * a); // OK foo(1, a => a * a); // Error }I forgot there is int here, so T in lambda should be deduced as int as well.With the latest alpha compiler it gives: test.d(4): Error: template test.foo does not match any function template declaration. Candidates are: test.d(1): test.foo(T)(T x, T function(T) f) test.d(4): Error: template test.foo(T)(T x, T function(T) f) cannot deduce template function from argument types !()(int,void) Do you think it's a good idea to allow it? Bye, bearophileDoes makes sense.
Dec 06 2012
Doesn't that mean you're asking the compiler to have a full unification engine? Your example is simple, but the general case is more complicated: void foo(T,U,V)(Tuple!(T,U) tup, V function(U) fun) { ... } void main() { foo(tuple(1,"abc"), (a) => 1.34); // so, I can deduce V to be double foo(tuple(1,"abc"), (int i) => i+); // error, U unified to string and int! }
Dec 06 2012
Philippe Sigaud:Doesn't that mean you're asking the compiler to have a full unification engine? Your example is simple, but the general case is more complicated:I think the general case doesn't require a whole program type inference, just a local one. But maybe it's too much complex to do it in D :-) I don't know. Bye, bearophile
Dec 06 2012
On 12/06/2012 02:04 PM, bearophile wrote:Currently this code is not supported: void foo(T)(T x, T function(T) f) {} void main() { foo(1, (int a) => a * a); // OK foo(1, a => a * a); // Error } With the latest alpha compiler it gives: test.d(4): Error: template test.foo does not match any function template declaration. Candidates are: test.d(1): test.foo(T)(T x, T function(T) f) test.d(4): Error: template test.foo(T)(T x, T function(T) f) cannot deduce template function from argument types !()(int,void) Do you think it's a good idea to allow it? Bye, bearophileI consider it necessary.
Dec 07 2012
Timon Gehr:I consider it necessary.To consider opening an enhancement request, are you able to explain us why? And is it possible to implement it? :-) Bye, bearophile
Dec 07 2012
On 12/07/2012 11:12 PM, bearophile wrote:Timon Gehr:It is just embarrassing if it does not work. Similar things work even in complete and stuff. I think we want to support things like: [1,2,3].map(a=>2*a);I consider it necessary.To consider opening an enhancement request, are you able to explain us why?And is it possible to implement it? :-)Yes; in fact it almost works satisfactorily in my home-grown front end already. As most of D compilation, it is undecidable, but the following heuristic should be fine: 1. Resolve and fix explicit template arguments. 2. Infer types from arguments. Unify. Do not consider known lambda return types at this point. 3. Analyze lambdas whose parameter types are known. Fix those types. 4. Resolve lambda return values where known. Unify. 5. Repeat 2-4 until fixed point is reached. 6. Use default template arguments. 7. Repeat 5-6 until fixed point is reached. 8. Check if everything became known and if the argument types are actually compatible with the inferred signature. (Where 'fix' means, if the type should later be unified with something that is not the same type, fail instantiation.) Furthermore, probably it should treat curried lambdas a little more cleverly than the above. It may be argued that this is too complex. :o)
Dec 07 2012
Timon Gehr:I think we want to support things like: [1,2,3].map(a=>2*a);This was one of my original use cases.It may be argued that this is too complex. :o)Even if they are trying to speed up Scala compilation, it's much slower than Java/D compilation, because the Scala type system is very complex and refined and the compiler has to work a lot. If the D type system gets refined as we are discussing here, maybe the compilation speed of templates will be reduced a little. I have no idea if this is true, and how much. Bye, bearophile
Dec 07 2012