Bug 12454 – Return type inference does not work

Status
RESOLVED
Resolution
DUPLICATE
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Mac OS X
Creation time
2014-03-24T13:47:00Z
Last change time
2014-03-24T14:56:12Z
Assigned to
nobody
Creator
yuriy.glukhov

Comments

Comment #0 by yuriy.glukhov — 2014-03-24T13:47:54Z
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()
Comment #1 by destructionator — 2014-03-24T13:52:52Z
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)
Comment #2 by andrej.mitrovich — 2014-03-24T13:56:14Z
D does not support return type overloading. I'm not sure whether this is mentioned in the spec though. Others can chime in.
Comment #3 by yuriy.glukhov — 2014-03-24T14:01:06Z
(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?
Comment #4 by andrej.mitrovich — 2014-03-24T14:03:34Z
(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.
Comment #5 by yuriy.glukhov — 2014-03-24T14:06:35Z
I see, thanx for clarification.
Comment #6 by andrej.mitrovich — 2014-03-24T14:07:37Z
(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.
Comment #7 by yuriy.glukhov — 2014-03-24T14:26:53Z
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.
Comment #8 by andrej.mitrovich — 2014-03-24T14:34:24Z
*** This issue has been marked as a duplicate of issue 2999 ***
Comment #9 by andrej.mitrovich — 2014-03-24T14:56:12Z
(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)