Bug 22862 – Functions cannot be overloaded on return value alone.

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2022-03-09T06:08:26Z
Last change time
2022-03-09T12:41:05Z
Keywords
pull
Assigned to
No Owner
Creator
FeepingCreature

Comments

Comment #0 by default_357-line — 2022-03-09T06:08:26Z
If you attempt to write code like this: int foo() { } float foo() { } you get an error: foo conflicts with previous declaration. Allowing this seems clearly pointless, since overloads that only differ by return value cannot be differentiated on call, but they *can* be differentiated by traits, ie. getOverloads. As such, it would be nice if the arbitrary restriction on overloading functions with the same parameter types was removed.
Comment #1 by default_357-line — 2022-03-09T06:16:57Z
To clarify, what I'm asking is to remove: test.d(2): Error: function `test.foo()` conflicts with previous declaration at test.d(1) And rely on: test.d(4): Error: `test.foo` called with argument types `()` matches both: test.d(1): `test.foo()` and: test.d(2): `test.foo()` At which point you could explicitly call one like so: int foo() { return 0; } float foo() { return 0f; } void main() { // auto a = foo; auto a = __traits(getOverloads, test, "foo")[0](); } Which would allow you to select which one to call based on ReturnType!T. The background for this is that right now, in my serialization framework I'm defining serialization functions for various types as normal overloaded functions where the function that is called is chosen on parameter type, ie. I might have JSONValue encode(Foo), JSONValue encode(Bar). But on the return trip, because both decode functions: Foo decode(JSONValue), Bar decode(JSONValue), take JSONValue, I cannot overload and have to use the awkward hack Foo decode(T : Foo)(JSDNValue) which keeps causing issues. If I could just *define* foo, even if I couldn't call it directly, all would be well.
Comment #2 by dlang-bot — 2022-03-09T07:21:07Z
@FeepingCreature created dlang/dmd pull request #13787 "Fix issue 22862: Remove error when overloading two D functions with the same parameters. (Change)" fixing this issue: - Fix issue 22862: Remove error when overloading two D functions with the same parameters. https://github.com/dlang/dmd/pull/13787