www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Explicit Interface Implementation

reply "ref2401" <refactor24 gmail.com> writes:
Does D provide a way for explicit interface implementation?
[C#] https://msdn.microsoft.com/en-us/library/ms173157.aspx
Feb 14 2015
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 02/14/2015 09:19 AM, ref2401 wrote:
 Does D provide a way for explicit interface implementation?
 [C#] https://msdn.microsoft.com/en-us/library/ms173157.aspx
Apparently, yes: interface A { int foo(); } interface B { int foo(); } class C : A, B { int foo() { return 42; } } void main() { auto c = new C(); A a = c; B b = c; assert(a.foo() == 42); assert(b.foo() == 42); } Ali
Feb 14 2015
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Saturday, 14 February 2015 at 20:08:09 UTC, Ali Çehreli wrote:
     int foo()
     {
         return 42;
     }
the difference is C# also allows you to implement A.foo separately from B.foo. I don't think D allows that. You can call a specific interface at the usage site, but implementing the function differently is something I don't think we can do.
Feb 14 2015
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 02/14/2015 12:13 PM, Adam D. Ruppe wrote:
 On Saturday, 14 February 2015 at 20:08:09 UTC, Ali Çehreli wrote:
     int foo()
     {
         return 42;
     }
the difference is C# also allows you to implement A.foo separately from B.foo. I don't think D allows that.
Yeah, I obviously read too little of the C# document. Basically, I implemented their presentation of the problem. :) Ali
Feb 14 2015
prev sibling parent Ivan Timokhin <timokhin.iv gmail.com> writes:
ref2401 wrote:

 Does D provide a way for explicit interface implementation?
 [C#] https://msdn.microsoft.com/en-us/library/ms173157.aspx
Not exactly, but you may try something like this: interface A { void foo(); } interface B { void foo(); } class C : A { class Nested : B { void foo() { this.outer.fooB(); } } Nested nested; alias nested this; this() { nested = this.new Nested; } void foo() { import std.stdio : writeln; writeln("A's foo"); } void fooB() { import std.stdio : writeln; writeln("B's foo"); } } void main() { C c = new C; A a = c; B b = c; a.foo(); b.foo(); } Prints: A's foo B's foo
Feb 14 2015