Bug 9555 – Type deduction for new lambda syntax literals breaks with templates

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-02-21T03:06:00Z
Last change time
2013-02-21T13:33:15Z
Assigned to
nobody
Creator
public

Comments

Comment #0 by public — 2013-02-21T03:06:35Z
Simple motivating example: --- test.d --- import std.functional; void main() { auto deleg = toDelegate(a => a > 2); } ------ --- shell --- $ rdmd test.d test.d(5): Error: template std.functional.toDelegate does not match any function template declaration. Candidates are: /usr/include/dmd/phobos/std/functional.d(722): std.functional.toDelegate(F)(auto ref F fp) if (isCallable!(F)) test.d(5): Error: template std.functional.toDelegate(F)(auto ref F fp) if (isCallable!(F)) cannot deduce template function from argument types !()(void) ------ Type of lambda was deduced as "void" here.
Comment #1 by maxim — 2013-02-21T04:29:03Z
Actually type of lambda was not deduced to void, void here is pseudo type of non-instantiated template, because a => a > 2 is a lambda template. If you append type of a parameter, this would work, for ex: import std.functional; void main() { auto deleg = toDelegate( (int a) => a > 2); } Since there is no guesses what type of "a" can be in the original code, template cannot be instantiated. By the way, idea mentioned in forum discussion that there is problem with new (lambda) syntax is also wrong, because the code can be rewritten with delegate template: import std.functional; void main() { auto deleg = toDelegate( delegate (a) { return a > 2; } ); } with the same problem and same error message. Close this as invalid.
Comment #2 by bioinfornatics — 2013-02-21T04:39:49Z
but if your delegate look like : in bool delegate(in size_t) lambda you can't use (in int a) => a > 2) or (const int a) => a > 2)
Comment #3 by bioinfornatics — 2013-02-21T04:41:32Z
skip my comment that is allowed to do ((in int a) => a > 2)
Comment #4 by public — 2013-02-21T08:18:45Z
Waa, it is a template? Unexpected. What about turning this into enhancement request to improve error message though? Something like "not enough context to deduce lambda type".
Comment #5 by maxim — 2013-02-21T09:26:44Z
(In reply to comment #4) > Waa, it is a template? Unexpected. What about turning this into enhancement > request to improve error message though? Something like "not enough context to > deduce lambda type". Enough context can be provided later. template get(alias a) { alias a!int get; } template foo(fun...) { alias get!fun foo; } void main() { alias foo!(a=>a) a; assert(a(1) == 1); assert(a(1.0) is 1.0); //NG } Note, this is reduced from how map and unaryfun works. If you turn it into context-independent error, this would mean code break for some usages which are currently accepted.
Comment #6 by public — 2013-02-21T13:33:15Z
I was speaking exactly about the case when it is used and context is lacking. This message "cannot deduce template function from argument types !()(void)" is very misleading for someone who is not aware of lambda implementation inner details.