www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Default implementations in inherited interfaces

reply Saurabh Das <saurabh.das gmail.com> writes:
I have an interface A which declares a certain function. A second 
interface B inherits from A and wishes to provide a default 
implementation for that function. How can I achieve this? I'm 
facing an error when I try this:

interface A
{
     int func(int);
}

interface B : A
{
     final int func(int)
     {
         return 0;
     }
}

class C : B
{
}

rdmd it.d:
it.d(14): Error: class it.C interface function 'int func(int)' is 
not implemented

Thanks,
Saurabh
Jul 21 2016
next sibling parent reply Lodovico Giaretta <lodovico giaretart.net> writes:
On Thursday, 21 July 2016 at 09:41:27 UTC, Saurabh Das wrote:
 I have an interface A which declares a certain function. A 
 second interface B inherits from A and wishes to provide a 
 default implementation for that function. How can I achieve 
 this? I'm facing an error when I try this:

 interface A
 {
     int func(int);
 }

 interface B : A
 {
     final int func(int)
     {
         return 0;
     }
 }

 class C : B
 {
 }

 rdmd it.d:
 it.d(14): Error: class it.C interface function 'int func(int)' 
 is not implemented

 Thanks,
 Saurabh
Interesting. This is worth a bugzilla issue, IMHO. In fact, if you try the other way (i.e.: you provide an implementation of func in class C), you get the opposite error, that you are overriding a final function (B.func).
Jul 21 2016
parent Lodovico Giaretta <lodovico giaretart.net> writes:
On Thursday, 21 July 2016 at 09:46:10 UTC, Lodovico Giaretta 
wrote:
 Interesting.
 This is worth a bugzilla issue, IMHO. In fact, if you try the 
 other way (i.e.: you provide an implementation of func in class 
 C), you get the opposite error, that you are overriding a final 
 function (B.func).
Submitted as issue 16306 https://issues.dlang.org/show_bug.cgi?id=16306
Jul 21 2016
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 21 July 2016 at 09:41:27 UTC, Saurabh Das wrote:
 I have an interface A which declares a certain function. A 
 second interface B inherits from A and wishes to provide a 
 default implementation for that function.
You can't, interfaces cannot have implementations of virtual functions. Your code is trying to define two separate functions with the same name (the compiler should probably prohibit that, since it doesn't do want you want it to do). Your best bet is to make B an abstract class instead of an interface. Then it can override interface functions. Of course, it will also tie you down as you can only inherit from one class, but it will actually work.
Jul 21 2016
parent reply Saurabh Das <saurabh.das gmail.com> writes:
On Thursday, 21 July 2016 at 12:42:14 UTC, Adam D. Ruppe wrote:
 On Thursday, 21 July 2016 at 09:41:27 UTC, Saurabh Das wrote:
 I have an interface A which declares a certain function. A 
 second interface B inherits from A and wishes to provide a 
 default implementation for that function.
You can't, interfaces cannot have implementations of virtual functions. Your code is trying to define two separate functions with the same name (the compiler should probably prohibit that, since it doesn't do want you want it to do). Your best bet is to make B an abstract class instead of an interface. Then it can override interface functions. Of course, it will also tie you down as you can only inherit from one class, but it will actually work.
I see. Java 8 has a 'default' keyword that allows interfaces to provide a default implementation and sub-classes can optionally override it if needed. The rationale behind it was extending interfaces without causing old code to faill. (called "virtual extension methods" or "defender methods"). The use case is similar to above. Is there a way to achieve an equivalent functionality in D? Thanks, Saurabh
Jul 21 2016
parent reply Jonathan Marler <johnnymarler gmail.com> writes:
On Thursday, 21 July 2016 at 13:37:30 UTC, Saurabh Das wrote:
 On Thursday, 21 July 2016 at 12:42:14 UTC, Adam D. Ruppe wrote:
 On Thursday, 21 July 2016 at 09:41:27 UTC, Saurabh Das wrote:
Java 8 has a 'default' keyword that allows interfaces to provide a default implementation and sub-classes can optionally override it if needed. The rationale behind it was extending interfaces without causing old code to faill. (called "virtual extension methods" or "defender methods"). The use case is similar to above. Is there a way to achieve an equivalent functionality in D? Thanks, Saurabh
What an interesting technique. I've never seen this before. Maybe a DIP is in order? I think it would be low priority relative to the current work being done, but this technique seems like a good thing to support in the language.
Jul 24 2016
next sibling parent Antonio Corbi <amcb ggmail.com> writes:
On Sunday, 24 July 2016 at 07:54:11 UTC, Jonathan Marler wrote:
 On Thursday, 21 July 2016 at 13:37:30 UTC, Saurabh Das wrote:
 On Thursday, 21 July 2016 at 12:42:14 UTC, Adam D. Ruppe wrote:
 On Thursday, 21 July 2016 at 09:41:27 UTC, Saurabh Das wrote:
Java 8 has a 'default' keyword that allows interfaces to provide a default implementation and sub-classes can optionally override it if needed. The rationale behind it was extending interfaces without causing old code to faill. (called "virtual extension methods" or "defender methods"). The use case is similar to above. Is there a way to achieve an equivalent functionality in D? Thanks, Saurabh
What an interesting technique. I've never seen this before. Maybe a DIP is in order? I think it would be low priority relative to the current work being done, but this technique seems like a good thing to support in the language.
I first heard about this technique (or similar) in this post by Jim Nelson about the Vala language: https://blogs.gnome.org/jnelson/2011/11/01/a-few-of-my-favorite-vala-things-interface/ Antonio
Jul 24 2016
prev sibling parent Saurabh Das <saurabh.das gmail.com> writes:
On Sunday, 24 July 2016 at 07:54:11 UTC, Jonathan Marler wrote:
 On Thursday, 21 July 2016 at 13:37:30 UTC, Saurabh Das wrote:
 On Thursday, 21 July 2016 at 12:42:14 UTC, Adam D. Ruppe wrote:
 On Thursday, 21 July 2016 at 09:41:27 UTC, Saurabh Das wrote:
Java 8 has a 'default' keyword that allows interfaces to provide a default implementation and sub-classes can optionally override it if needed. The rationale behind it was extending interfaces without causing old code to faill. (called "virtual extension methods" or "defender methods"). The use case is similar to above. Is there a way to achieve an equivalent functionality in D? Thanks, Saurabh
What an interesting technique. I've never seen this before. Maybe a DIP is in order? I think it would be low priority relative to the current work being done, but this technique seems like a good thing to support in the language.
I am studying the use cases for defender methods. It would be good to support this in D. I don't think I have enough knowledge about the subject to write a DIP for it yet though.
Jul 25 2016