Bug 5060 – Order of interface implementations affects code
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-10-15T12:10:00Z
Last change time
2015-04-02T09:33:51Z
Assigned to
nobody
Creator
andrej.mitrovich
Comments
Comment #0 by andrej.mitrovich — 2010-10-15T12:10:27Z
Code:
interface Foo
{
final void run() { writeln("foo"); }
}
interface Bar
{
final void run() { writeln("bar"); }
}
class One : Foo, Bar
{
}
class Two : Bar, Foo
{
}
void main()
{
with (new One)
{
run(); // writes foo
}
with (new Two)
{
run(); // writes bar
}
}
Comment #1 by andrej.mitrovich — 2013-09-17T13:05:41Z
P.S.: OP code is missing an import to std.stdio.
Perhaps the right way to implement this is to simply allow shadowing via an alias declaration:
class One : Foo, Bar
{
alias Foo.run run; // pick Foo's run, hide Bar's run
}
class Two : Bar, Foo
{
alias Bar.run run; // do the opposite
}
Otherwise, emit a compiler error. Thoughts?
Comment #2 by andrej.mitrovich — 2013-09-17T13:07:28Z
(In reply to comment #1)
> Otherwise, emit a compiler error.
I meant emit the error at the call site when "run" is called but both interfaces define the function with this name. Either the class developer introduces the alias, or the user could do:
object.Foo.run(); // explicitly call this interface function
object.Bar.run(); // ditto
Comment #3 by verylonglogin.reg — 2015-04-02T09:33:51Z
*** This issue has been marked as a duplicate of issue 4435 ***