Pathological import symbol shadowing
Paul Backus
snarwin at gmail.com
Sat Nov 14 16:25:07 UTC 2020
On Saturday, 14 November 2020 at 15:48:47 UTC, Kagamin wrote:
> On Saturday, 14 November 2020 at 14:43:16 UTC, Paul Backus
> wrote:
>> [1] https://dlang.org/spec/function.html#overload-sets
>
> Ah, ok.
>
> foo(1L); // calls A.foo(long)
>
> This line looks incorrect. If there was no foo(long), foo(1L)
> would call foo(int), then if foo(long) appears, the call will
> be silently diverted.
Yes, it is incorrect. In reality, it produces an error:
Error: function B.foo at B.d(4) conflicts with function A.foo
at A.d(3)
However, the reason for this is that the literal `1L` is treated
as both an int and a long for the purposes of overload
resolution, because the compiler can prove that its value will
fit into both types. If you change it to a variable:
long l;
foo(l);
...then A.foo(long) is selected unambiguously, and removing it
will cause an error ("no overload matches") rather than calling
B.foo(int).
More information about the Digitalmars-d
mailing list