digitalmars.D - Inheritance from multiple interfaces with the same method name
- Q. Schroll (23/23) Dec 06 2017 Say I have two interfaces
- Pham (13/37) Dec 06 2017 Delphi resolves this with below syntax; I think it's clean and
- Mike Franklin (22/24) Dec 06 2017 It seems it's allowed, but the caller is required to disambiguate.
- Adam D. Ruppe (5/11) Dec 07 2017 D also allows you to simply write:
- jmh530 (11/16) Dec 07 2017 It would be nice if you could do something like below (I know you
- Q. Schroll (6/20) Dec 07 2017 This implies that I cannot implement two syntactically identical
- Mike Franklin (7/12) Dec 07 2017 Yes, you are right. I think it would be nice if D supported a
- Q. Schroll (6/8) Dec 07 2017 Sounds more like a DIP to me. There is no way to enable this
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
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.htmlDelphi 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
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
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
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 btwIt 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
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: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.// 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
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
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