struct A { int foo(string s) { return 0; } } static assert(__traits(getOverloads, A.init, "foo")[0]("hi") == 0); // works static assert(__traits(getOverloads, A.init, "foo", true)[0]("hi") == 0); // Error: need this for foo of type int(string s) struct B { int foo()(int i) { return 1; } int foo(string s) { return 0; } } static assert(__traits(getOverloads, B.init, "foo")[0]("hi") == 0); // Error: need this for foo of type int(string s) static assert(__traits(getOverloads, B.init, "foo", true)[0]("hi") == 0); // Error: need this for foo of type int(string s) static assert(__traits(getOverloads, B.init, "foo", true)[1](7) == 1); // Error: need this for foo of type pure nothrow @nogc @safe int(int i) struct C { static int foo()(int i) { return 1; } int foo(string s) { return 0; } } static assert(__traits(getOverloads, C.init, "foo")[0]("hi") == 0); // Error: need this for foo of type int(string s) static assert(__traits(getOverloads, C.init, "foo", true)[0]("hi") == 0); // Error: need this for foo of type int(string s) static assert(__traits(getOverloads, C, "foo", true)[1](7) == 1); // works
Ignore the third assert for struct B, that's kind of unrelated, more similar to https://issues.dlang.org/show_bug.cgi?id=18969
@rmanthorpe created dlang/dmd pull request #11432 "Fix Issue 21058 - __traits(getOverloads) forgets "this" with third ar…" fixing this issue: - Fix Issue 21058 - __traits(getOverloads) forgets "this" with third argument or if first overload is a template https://github.com/dlang/dmd/pull/11432
dlang/dmd pull request #11432 "Fix Issue 21058 - __traits(getOverloads) forgets "this" with third ar…" was merged into master: - 5d78bf038f3848898f1f184141ab2349025ecaba by Richard Manthorpe: Fix Issue 21058 - __traits(getOverloads) forgets "this" with third argument or if first overload is a template When the target of `getOverloads` is a `dotTemplateDeclaration` we weren't propagating the context to the overloads. Similarly when including templates we weren't propagating the context even if we had one. This fixed both those problems. https://github.com/dlang/dmd/pull/11432