Thread overview
Default implementations in inherited interfaces
Jul 21, 2016
Saurabh Das
Jul 21, 2016
Lodovico Giaretta
Jul 21, 2016
Lodovico Giaretta
Jul 21, 2016
Adam D. Ruppe
Jul 21, 2016
Saurabh Das
Jul 24, 2016
Jonathan Marler
Jul 24, 2016
Antonio Corbi
Jul 25, 2016
Saurabh Das
July 21, 2016
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

July 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,
> 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).
July 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
July 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.

July 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 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

July 24, 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:
>> 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.
July 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:
>> 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
July 25, 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:
>> 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.