Aliasing multiple delegates to the same name - very strange behaviour

Meta jared771 at gmail.com
Sun Feb 25 09:04:04 UTC 2018


On Sunday, 25 February 2018 at 08:07:03 UTC, user1234 wrote:
> On Sunday, 25 February 2018 at 05:16:21 UTC, Meta wrote:
>> On Sunday, 25 February 2018 at 04:59:58 UTC, Basile B. wrote:
>>> Use templates to prevent implicit conversion:
>>>
>>> alias f(T = int) = (T n) => 0;
>>> alias f(T = char) = (T n) => 'a';
>>> alias f(T = bool) = (T n) => false;
>>>
>>> Bug report is invalid and can be closed.
>>
>> Please don't be so hasty. The main focus of that defect is 
>> whether it is a bug or a feature that the same alias can be 
>> declared multiple times. I've updated the title to reflect 
>> that.
>
> Aliases are not things, they are what they alias. In your case 
> all are functions so this is an overload set.

I was about to say that no such syntax for creating an overload 
set exists, but I found this tucked away in the documentation 
(https://dlang.org/spec/function.html#overload-sets):

Overload sets can be merged with an alias declaration:

import A;
import B;

alias foo = A.foo;
alias foo = B.foo;

void bar(C c)
{
     foo();    // calls A.foo()
     foo(1L);  // calls A.foo(long)
     foo(c);   // calls B.foo(C)
     foo(1,2); // error, does not match any foo
     foo(1);   // calls B.foo(int)
     A.foo(1); // calls A.foo(long)
}

So it looks like this *is* valid syntax, in which case the 
question becomes, is it intended behaviour that you can use the 
overload set syntax with function literals? I can't think of any 
possible method of assigning the same name to different literals. 
Where is the bug? Being able to add function literals to an 
overload set, or the fact that the set only contains the first 
function and none of the subsequently added ones?


More information about the Digitalmars-d mailing list