Thread overview
[Issue 12454] New: Return type inference does not work
Mar 24, 2014
Yuriy
Mar 24, 2014
Adam D. Ruppe
Mar 24, 2014
Andrej Mitrovic
Mar 24, 2014
Yuriy
Mar 24, 2014
Andrej Mitrovic
Mar 24, 2014
Yuriy
Mar 24, 2014
Andrej Mitrovic
Mar 24, 2014
Yuriy
Mar 24, 2014
Andrej Mitrovic
Mar 24, 2014
Andrej Mitrovic
March 24, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12454

           Summary: Return type inference does not work
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: Mac OS X
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: yuriy.glukhov@gmail.com


--- Comment #0 from Yuriy <yuriy.glukhov@gmail.com> 2014-03-24 13:47:54 PDT ---
The code below will not compile with dmd v2.066-devel-f230a39-dirty.

string deduceRetType() {
    return "1";
}

int deduceRetType() {
    return 1;
}

void main() {
    int qwe = deduceRetType();
}

Compiler errors:
rettype.d(15): Error: rettype.deduceRetType called with argument types ()
matches both:
rettype.d(3):     rettype.deduceRetType()
and:
rettype.d(8):     rettype.deduceRetType()

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 24, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12454



--- Comment #1 from Adam D. Ruppe <destructionator@gmail.com> 2014-03-24 13:52:52 PDT ---
D doesn't allow overloading on the return type, only on argument types. The reason is that it would be difficult to figure out which one you want:

auto a = deduceRetType(); // which one?

int a = 10 + deduceRetType(); // which one?

Those could arguably be ambiguous errors or the second one might say string doesn't make sense, so use int, but that's not how D works; an expression's type is always known by looking at the expression itself without considering if it works in context or not.

(if it doesn't work in context, that's when a compile error for mismatched type
is triggered)

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 24, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12454


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |andrej.mitrovich@gmail.com
         Resolution|                            |INVALID


--- Comment #2 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2014-03-24 13:56:14 PDT ---
D does not support return type overloading. I'm not sure whether this is mentioned in the spec though. Others can chime in.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 24, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12454



--- Comment #3 from Yuriy <yuriy.glukhov@gmail.com> 2014-03-24 14:01:06 PDT ---
(In reply to comment #2)
> D does not support return type overloading. I'm not sure whether this is mentioned in the spec though. Others can chime in.

In that case, it should not be possible to define two functions with just ret type difference. Or is there a way to explicitly call one or another?

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 24, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12454



--- Comment #4 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2014-03-24 22:03:34 CET ---
(In reply to comment #3)
> (In reply to comment #2)
> > D does not support return type overloading. I'm not sure whether this is mentioned in the spec though. Others can chime in.
> 
> In that case, it should not be possible to define two functions with just ret type difference.

Currently the compiler doesn't implement these checks, but arguably it should. For example the following will issue a linker error:

-----
class Foo
{
    void test() { }
    void test() { }
}

void main()
{
}
-----

Implementing checks is an enhancement that is already filed somewhere in bugzilla.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 24, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12454



--- Comment #5 from Yuriy <yuriy.glukhov@gmail.com> 2014-03-24 14:06:35 PDT ---
I see, thanx for clarification.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 24, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12454



--- Comment #6 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2014-03-24 22:07:37 CET ---
(In reply to comment #4)
> Implementing checks is an enhancement that is already filed somewhere in bugzilla.

Actually I think I was wrong. Issue 6533 (duplicate *overrides*) is what I was probably remembering, but I can't find any bugs filed for duplicate overloads.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 24, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12454



--- Comment #7 from Yuriy <yuriy.glukhov@gmail.com> 2014-03-24 14:26:53 PDT ---
Ok, i think i gave up too early. Do you know where i could get more info on the
rettype deduction topic? I still can't understand why this feature was not
implemented. From the first glance it seems to me pretty obvious, which
function to select:
1. If there is one function that compiles, select it.
2. Else if there is more than one function that compiles, emit an ambiguity
error. Maybe this logic could have been expanded to something more complex as
cast-path-length etc, but even such basic support would make it really useful.
Please correct me if I'm wrong.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 24, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12454


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|INVALID                     |DUPLICATE


--- Comment #8 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2014-03-24 22:34:24 CET ---
*** This issue has been marked as a duplicate of issue 2999 ***

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 24, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12454



--- Comment #9 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2014-03-24 22:56:12 CET ---
(In reply to comment #7)
> Ok, i think i gave up too early. Do you know where i could get more info on the rettype deduction topic?

There were many such topics. You can do a search in the DForums for "return type overloading":

http://forum.dlang.org/ (top-right corner)

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