[Issue 22862] Functions cannot be overloaded on return value alone.

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Mar 9 06:16:57 UTC 2022


https://issues.dlang.org/show_bug.cgi?id=22862

--- Comment #1 from FeepingCreature <default_357-line at yahoo.de> ---
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.

--


More information about the Digitalmars-d-bugs mailing list