Aliasing multiple delegates to the same name - very strange behaviour
Meta
jared771 at gmail.com
Sun Feb 25 04:06:43 UTC 2018
I just filed this bug:
https://issues.dlang.org/show_bug.cgi?id=18520
Not only does the following code compile and link successfully,
it prints 0 three times when ran:
alias f = (int n) => 0;
alias f = (char c) => 'a';
alias f = (bool b) => false;
void main()
{
import std.stdio;
writeln(f(int.init)); //Prints 0
writeln(f(char.init)); //Prints 0
writeln(f(bool.init)); //Prints 0
}
However, when I change the code to the following, it works as one
could reasonably expect, given the circumstances:
int f1(int n) { return 0; }
char f2(char c) { return 'a'; }
bool f3(bool b) { return false; }
alias f = f1;
alias f = f2;
alias f = f3;
void main()
{
import std.stdio;
writeln(f(int.init)); //Prints 0
writeln(f(char.init)); //Prints 'a'
writeln(f(bool.init)); //Prints false
}
I've got some questions:
1. Which is the intended behaviour? Should this code fail to
compile and there's a bug with aliases, or should this code
compile and my first example work correctly, but there is
currently a bug where this feature interacts badly with
function/delegate literals?
2. If the answer to 1 is "this could should compile and work
correctly", in what cases does D allow multiple aliases with the
same name to be defined, as in my first and second example (which
compile without issue)?
3. Is this related to overload sets in some way?
4. Is there any different semantically or mechanically between my
first and second examples?
More information about the Digitalmars-d
mailing list