www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Inheritance from multiple interfaces with the same method name

reply Q. Schroll <qs.il.paperinik gmail.com> writes:
Say I have two interfaces
     interface I { void f(); }
and
     interface J {  int f(); }
implemented by some class
     class A : I, J {
         // challenge by the compiler:
         // implement f()!
     }

VB.NET allows that by renaming the implementation (it does allow 
it generally, not only in the corner case).

implementing (can be omitted for exactly one; corner case 
handling); the specification makes the implementation private. 
(See [1])
Java just disallows the case when two methods are incompatible. 
If they are compatible, they must be implemented by the same 
method. If they are meant to do different things, you are screwed.

What is D's position on that? The interface spec [2] does not say 
anything about that case.


[1] 
https://stackoverflow.com/questions/2371178/inheritance-from-multiple-interfaces-with-the-same-method-name
[2] https://dlang.org/spec/interface.html
Dec 06 2017
next sibling parent Pham <home home.com> writes:
On Wednesday, 6 December 2017 at 23:56:33 UTC, Q. Schroll wrote:
 Say I have two interfaces
     interface I { void f(); }
 and
     interface J {  int f(); }
 implemented by some class
     class A : I, J {
         // challenge by the compiler:
         // implement f()!
     }

 VB.NET allows that by renaming the implementation (it does 
 allow it generally, not only in the corner case).

 implementing (can be omitted for exactly one; corner case 
 handling); the specification makes the implementation private. 
 (See [1])
 Java just disallows the case when two methods are incompatible. 
 If they are compatible, they must be implemented by the same 
 method. If they are meant to do different things, you are 
 screwed.

 What is D's position on that? The interface spec [2] does not 
 say anything about that case.


 [1] 
 https://stackoverflow.com/questions/2371178/inheritance-from-multiple-interfaces-with-the-same-method-name
 [2] https://dlang.org/spec/interface.html
Delphi resolves this with below syntax; I think it's clean and simple Pham class A : I, J { // Define function for each interface void I_f() {} int J_f() {} // Assign function to interface I.f = I_f; J.f = J_f; }
Dec 06 2017
prev sibling parent reply Mike Franklin <slavo5150 yahoo.com> writes:
On Wednesday, 6 December 2017 at 23:56:33 UTC, Q. Schroll wrote:

 What is D's position on that? The interface spec [2] does not 
 say anything about that case.
It seems it's allowed, but the caller is required to disambiguate. import std.stdio; interface I { void f(); } interface J { int f(); } class A : I, J { void f() { writeln("void f()"); } int f() { writeln("int f()"); return 0; } } void main() { A a = new A(); // Error: A.f called with argument types () matches both: A.f() and A.f() // Yeah, that error message could be better. //a.f(); (cast(I)a).f(); // prints "void f()" (cast(J)a).f(); // prints "int f()" } https://run.dlang.io/is/lZSblC Mike
Dec 06 2017
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 7 December 2017 at 00:45:21 UTC, Mike Franklin wrote:
     // Error: A.f called with argument types () matches both: 
 A.f() and A.f()
     // Yeah, that error message could be better.
     //a.f();

     (cast(I)a).f(); // prints "void f()"
     (cast(J)a).f(); // prints "int f()"
D also allows you to simply write: a.I.f(); a.J.f(); also works for explicitly calling a base class implementation btw
Dec 07 2017
next sibling parent jmh530 <john.michael.hall gmail.com> writes:
On Thursday, 7 December 2017 at 15:14:48 UTC, Adam D. Ruppe wrote:
 D also allows you to simply write:

 a.I.f();
 a.J.f();

 also works for explicitly calling a base class implementation 
 btw
It would be nice if you could do something like below (I know you can do something similar with std.conv.to). void main() { import std.stdio : writeln; float x = 2.5; writeln(cast(int)x); writeln(x.cast(int)); writeln(x.int); }
Dec 07 2017
prev sibling parent reply Q. Schroll <qs.il.paperinik gmail.com> writes:
On Thursday, 7 December 2017 at 15:14:48 UTC, Adam D. Ruppe wrote:
 On Thursday, 7 December 2017 at 00:45:21 UTC, Mike Franklin 
 wrote:
     // Error: A.f called with argument types () matches both: 
 A.f() and A.f()
     // Yeah, that error message could be better.
     //a.f();

     (cast(I)a).f(); // prints "void f()"
     (cast(J)a).f(); // prints "int f()"
D also allows you to simply write: a.I.f(); a.J.f(); also works for explicitly calling a base class implementation btw
This implies that I cannot implement two syntactically identical methods with different implementations, like if J had void f(); too, I cannot have different implementations for I.f() and J.f(). That would be relevant if they should behave differently e.g. if they have conflicting contracts.
Dec 07 2017
parent reply Mike Franklin <slavo5150 yahoo.com> writes:
On Thursday, 7 December 2017 at 21:12:30 UTC, Q. Schroll wrote:

 This implies that I cannot implement two syntactically 
 identical methods with different implementations, like if J had 
 void f(); too, I cannot have different implementations for 
 I.f() and J.f(). That would be relevant if they should behave 
 differently e.g. if they have conflicting contracts.
Yes, you are right. I think it would be nice if D supported a qualifying the names. But, it's a very rare situation. If you think D should support something like this, the first thing to do is to file a bug report. Mike
Dec 07 2017
parent Q. Schroll <qs.il.paperinik gmail.com> writes:
On Thursday, 7 December 2017 at 23:00:38 UTC, Mike Franklin wrote:
 If you think D should support something like this, the first 
 thing to do is to file a bug report.
Sounds more like a DIP to me. There is no way to enable this without some kind of nontrivial syntax. I'd go with the VB approach and have something like void foo1() alias I.foo { } but that's up to the time discussing the DIP.
Dec 07 2017