digitalmars.D.learn - Default implementations in inherited interfaces
- Saurabh Das (23/23) Jul 21 2016 I have an interface A which declares a certain function. A second
- Lodovico Giaretta (6/29) Jul 21 2016 Interesting.
- Lodovico Giaretta (4/9) Jul 21 2016 Submitted as issue 16306
- Adam D. Ruppe (9/12) Jul 21 2016 You can't, interfaces cannot have implementations of virtual
- Saurabh Das (10/22) Jul 21 2016 I see.
- Jonathan Marler (5/16) Jul 24 2016 What an interesting technique. I've never seen this before. Maybe
- Antonio Corbi (5/23) Jul 24 2016 I first heard about this technique (or similar) in this post by
- Saurabh Das (4/22) Jul 25 2016 I am studying the use cases for defender methods. It would be
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
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, SaurabhInteresting. 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
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
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
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 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, SaurabhI 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
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: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.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
Jul 24 2016
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: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/ AntonioOn Thursday, 21 July 2016 at 12:42:14 UTC, Adam D. Ruppe wrote: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.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
Jul 24 2016
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: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.On Thursday, 21 July 2016 at 12:42:14 UTC, Adam D. Ruppe wrote: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.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
Jul 25 2016