Thread overview
[Issue 9555] New: Type deduction for new lambda syntax literals breaks with templates
Feb 21, 2013
Dicebot
Feb 21, 2013
Maxim Fomin
Feb 21, 2013
bioinfornatics
Feb 21, 2013
bioinfornatics
Feb 21, 2013
Dicebot
Feb 21, 2013
Maxim Fomin
Feb 21, 2013
Dicebot
February 21, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9555

           Summary: Type deduction for new lambda syntax literals breaks
                    with templates
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: m.strashun@gmail.com


--- Comment #0 from Dicebot <m.strashun@gmail.com> 2013-02-21 03:06:35 PST ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 21, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9555


Maxim Fomin <maxim@maxim-fomin.ru> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |maxim@maxim-fomin.ru
         Resolution|                            |INVALID


--- Comment #1 from Maxim Fomin <maxim@maxim-fomin.ru> 2013-02-21 04:29:03 PST ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 21, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9555


bioinfornatics <bioinfornatics@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bioinfornatics@gmail.com


--- Comment #2 from bioinfornatics <bioinfornatics@gmail.com> 2013-02-21 04:39:49 PST ---
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)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 21, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9555



--- Comment #3 from bioinfornatics <bioinfornatics@gmail.com> 2013-02-21 04:41:32 PST ---
skip my comment that is allowed to do ((in int a) => a > 2)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 21, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9555



--- Comment #4 from Dicebot <m.strashun@gmail.com> 2013-02-21 08:18:45 PST ---
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".

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 21, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9555



--- Comment #5 from Maxim Fomin <maxim@maxim-fomin.ru> 2013-02-21 09:26:44 PST ---
(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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 21, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9555



--- Comment #6 from Dicebot <m.strashun@gmail.com> 2013-02-21 13:33:15 PST ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------