Consider:
import std.traits;
void fun(int);
void fun(int, int);
void gun(int, int);
void gun(int);
static assert(arity!fun == 1);
static assert(arity!gun == 2);
This code passes, indicating that only the first name in lexical order of an overload set is considered by arity. This is definitely surprising. Worst case the behavior should be documented, but a real solution would allow people to indicate which overload they're interested in.
Comment #1 by simen.kjaras — 2017-10-27T21:22:29Z
This happens for all templates in std.traits that deal with properties of functions. Making the templates fail when presented with an under-specified overload set would be a breaking change, but documenting the behavior is definitely worth it.
Phobos currently does not have good tools for working with overload sets - __traits(getOverloads) is what we've got, and finding a specific overload is manual work. In order to fix that part of the problem, I've created getOverloads and overloadFor: https://github.com/dlang/phobos/pull/5818
Comment #2 by simen.kjaras — 2018-01-28T01:59:19Z
*** Issue 18314 has been marked as a duplicate of this issue. ***
Comment #3 by dechcaudron+issues.dlang — 2018-01-28T05:37:01Z
> a real solution would allow people to indicate which overload they're interested in
Maybe allow something of the sort
static assert(arity!fun(int) == 1);
static assert(arity!gun(int, int) == 2);
?
I had reported this as a Phobos issue (see above), but since it has been marked as a duplicate for a dmd one, there's not much I can do. Compiler stuff is still magic to me, I'm afraid.
Comment #4 by simen.kjaras — 2018-01-28T15:20:24Z
(In reply to dechcaudron+dlang.issue.tracking from comment #3)
> Maybe allow something of the sort
>
> static assert(arity!fun(int) == 1);
> static assert(arity!gun(int, int) == 2);
arity is an interesting case here, since it's hard to get the correct overload without knowing the arity already. :p
The PR mentioned above would get the correct arity this way:
static assert(arity!(overloadFor!(fun, int)) == 1);
static assert(arity!(overloadFor!(gun, int, int)) == 2);
> I had reported this as a Phobos issue (see above), but since it has been
> marked as a duplicate for a dmd one, there's not much I can do. Compiler
> stuff is still magic to me, I'm afraid.
Fixing the issue in DMD would break a *lot* of code, and so isn't really feasible. I'm pretty sure it's not really desirable either, since the new semantics would be at least as weird as what we have.
You could say the root issue is in DMD - templates operate on the first element when passed an overload set. The problem can however be ameliorated in Phobos, by giving programmers a way to specify which overload they mean.
Comment #5 by robert.schadek — 2024-12-01T16:31:01Z