Thread overview
Implementing interface in the class hierarchy
Jul 14
Arek
Jul 15
Arek
July 14
According to language reference (part 'Interfaces') this code will not compile:

interface D
{
    int foo();
}

class A : D
{
    int foo() { return 1; }
}

class B : A, D <- Error: class B interface function 'foo' is not implemented
{
}

Because: 'A reimplemented interface must implement all the interface functions, it does not inherit them from a super class'.

Why?

Each B object 'is an' A object (and each cat 'is an' animal) so if A implements D, then B implements D too. Implementing D second time doesn't change the nature of A and B.

More over, another example (more practical because here, the D interface is going to be implemented only once):

interface D
{
    int foo();
}

class A
{
    int foo() { return 1; }
}

class B : A, D <- Error: class B interface function 'foo' is not implemented
{
}

Class A doesn't implement D, but it has the method satisfied the D interface.

Why I have to provide the explicit implementation of 'foo' in B class?

I cannot logically explain this property of Dlang's OOP. Anyone could?

Thanks in advance.
Arek


July 14
On 7/14/17 7:04 AM, Arek wrote:
> According to language reference (part 'Interfaces') this code will not compile:
> 
> interface D
> {
>      int foo();
> }
> 
> class A : D
> {
>      int foo() { return 1; }
> }
> 
> class B : A, D <- Error: class B interface function 'foo' is not implemented
> {
> }
> 
> Because: 'A reimplemented interface must implement all the interface functions, it does not inherit them from a super class'.
> 
> Why?

Not sure what the use case is. Effectively, this is the same:

class B : A {}

D d = new B; // works

> Each B object 'is an' A object (and each cat 'is an' animal) so if A implements D, then B implements D too. Implementing D second time doesn't change the nature of A and B.
> 
> More over, another example (more practical because here, the D interface is going to be implemented only once):
> 
> interface D
> {
>      int foo();
> }
> 
> class A
> {
>      int foo() { return 1; }
> }
> 
> class B : A, D <- Error: class B interface function 'foo' is not implemented
> {
> }

This is a different story. I think it is technically possible for the compiler to make this connection (it needs to populate the I vtable with the right call w/ thunk), but I don't think there's a way to do it.

This doesn't work:

class B : A, D
{
   alias A.foo foo;
}

Relevant enhancement request:
https://issues.dlang.org/show_bug.cgi?id=2565

-Steve
July 15
On Friday, 14 July 2017 at 12:31:49 UTC, Steven Schveighoffer wrote:
<cut>
> Relevant enhancement request:
> https://issues.dlang.org/show_bug.cgi?id=2565
>
> -Steve

So it looks like there are no rational arguments for such a language specification, and this behavior is derived from some aspect of the compiler implementation.

Thanks

Arek