Implementing interface in the class hierarchy

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jul 14 05:31:49 PDT 2017


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


More information about the Digitalmars-d-learn mailing list