Bug 18520 – The same alias can be declared multiple times if a function or function/delegate literal is aliased

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-02-25T03:51:57Z
Last change time
2018-02-25T12:13:49Z
Assigned to
No Owner
Creator
monkeyworks12

Comments

Comment #0 by monkeyworks12 — 2018-02-25T03:51:57Z
The following code compiles with DMD 2.070.2 and prints 0 three times: 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 } It looks like the int overload is getting called each time.
Comment #1 by monkeyworks12 — 2018-02-25T03:58:52Z
The code seemingly functions correctly when I change it as follows: 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 }
Comment #2 by b2.temp — 2018-02-25T05:02:32Z
This due to implicit conversion which has for effect to make the first overload always working. a way to overcome this: alias f(T = int) = (T n) => 0; alias f(T = char) = (T n) => 'a'; alias f(T = bool) = (T n) => false;
Comment #3 by monkeyworks12 — 2018-02-25T05:13:47Z
Re-opening this until I can get clarification on whether it is supposed to be an error or not to define the same alias 3 times. In my second example it compiles and works correctly, making me think that this is a feature, not a bug.
Comment #4 by ag0aep6g — 2018-02-25T12:13:49Z
*** This issue has been marked as a duplicate of issue 16099 ***