Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
December 06, 2017 Inheritance from multiple interfaces with the same method name | ||||
---|---|---|---|---|
| ||||
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). C# allows that by specifying the target interface when 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 |
December 07, 2017 Re: Inheritance from multiple interfaces with the same method name | ||||
---|---|---|---|---|
| ||||
Posted in reply to Q. Schroll | 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).
> C# allows that by specifying the target interface when 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;
}
|
December 07, 2017 Re: Inheritance from multiple interfaces with the same method name | ||||
---|---|---|---|---|
| ||||
Posted in reply to Q. Schroll | 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 |
December 07, 2017 Re: Inheritance from multiple interfaces with the same method name | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Franklin | 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
|
December 07, 2017 Re: Inheritance from multiple interfaces with the same method name | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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);
}
|
December 07, 2017 Re: Inheritance from multiple interfaces with the same method name | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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.
|
December 07, 2017 Re: Inheritance from multiple interfaces with the same method name | ||||
---|---|---|---|---|
| ||||
Posted in reply to Q. Schroll | 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 way to disambiguate the definitions like C# does, by fully 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
|
December 07, 2017 Re: Inheritance from multiple interfaces with the same method name | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Franklin | 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.
|
Copyright © 1999-2021 by the D Language Foundation