Order of interface implementations affects code

Jonathan M Davis jmdavisProg at gmx.com
Thu Oct 14 10:21:30 PDT 2010


On Thursday, October 14, 2010 10:14:25 Andrej Mitrovic wrote:
> Should the order in which you implement interfaces have an effect in your
> code? It seems to be that way when you have two functions with the same
> name in the different interfaces. Here's an example:
> 
> import std.stdio : writeln;
> 
> 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
>     }
> }
> 
> Bug? Or legitimate working code?

I'd have to pull out my copy of TDPL, but IIRC, it should not be legal to call 
run() directly because it's ambiguous. Assuming that dmd was working correctly 
on this issue, it wouldn't let this code compile. I believe that you'd have to 
do something like Foo.run() and Bar.run() to call run(). I'd have to check TDPL 
to be completely sure of the syntax though.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list