www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 12454] New: Return type inference does not work

reply d-bugmail puremagic.com writes:
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



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: -------
Mar 24 2014
next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12454




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: -------
Mar 24 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Mar 24 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12454





 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: -------
Mar 24 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12454




22:03:34 CET ---


 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: -------
Mar 24 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12454




I see, thanx for clarification.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 24 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12454




22:07:37 CET ---

 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: -------
Mar 24 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12454




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: -------
Mar 24 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12454


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

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



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: -------
Mar 24 2014
prev sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12454




22:56:12 CET ---

 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: -------
Mar 24 2014