www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - interface and class inheritance

reply "Oleg B" <code.viator gmail.com> writes:
[code]
import std.stdio;

interface A { void funcA(); }
class B { final void funcA() { writeln( "B.funcA()" ); } }

class C: B, A { }

void main()
{
     auto c = new C;
     c.funcA();
}
[code/]

$ dmd -run interface.d
interface.d(6): Error: class interface.C interface function 'void 
funcA()' is not implemented

if swap A and B
[code]
class C: A, B { }
[code/]

$ dmd -run interface.d
interface.d(6): Error: class interface.C base type must be 
interface, not interface.B

how to workaround this without change in class B and interface A?
Nov 14 2013
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 11/14/2013 01:20 PM, Oleg B wrote:
 [code]
 import std.stdio;

 interface A { void funcA(); }
 class B { final void funcA() { writeln( "B.funcA()" ); } }

 class C: B, A { }

 void main()
 {
      auto c = new C;
      c.funcA();
 }
 [code/]

 $ dmd -run interface.d
 interface.d(6): Error: class interface.C interface function 'void
 funcA()' is not implemented

 if swap A and B
 [code]
 class C: A, B { }
 [code/]

 $ dmd -run interface.d
 interface.d(6): Error: class interface.C base type must be interface,
 not interface.B

 how to workaround this without change in class B and interface A?
One way is to expose B through 'alias this' (not by inheritance) but it doesn't scale because currently only one 'alias this' is allowed. import std.stdio; interface A { void funcA(); } class B { final void funcA() { writeln( "B.funcA()" ); } } class C: A { B b; alias b this; this(){ b = new B(); } void funcA() { return b.funcA(); } } void main() { auto c = new C; c.funcA(); } Ali
Nov 14 2013
prev sibling next sibling parent reply "Agustin" <agustin.l.alvarez hotmail.com> writes:
On Thursday, 14 November 2013 at 21:20:57 UTC, Oleg B wrote:
 [code]
 import std.stdio;

 interface A { void funcA(); }
 class B { final void funcA() { writeln( "B.funcA()" ); } }

 class C: B, A { }

 void main()
 {
     auto c = new C;
     c.funcA();
 }
 [code/]

 $ dmd -run interface.d
 interface.d(6): Error: class interface.C interface function 
 'void funcA()' is not implemented

 if swap A and B
 [code]
 class C: A, B { }
 [code/]

 $ dmd -run interface.d
 interface.d(6): Error: class interface.C base type must be 
 interface, not interface.B

 how to workaround this without change in class B and interface 
 A?
Try interface A { final void funcA(); } class B { final void funcA() { writeln( "B.funcA()" ); } } class C: B, A { }
Nov 14 2013
parent reply "Agustin" <agustin.l.alvarez hotmail.com> writes:
On Thursday, 14 November 2013 at 21:42:38 UTC, Agustin wrote:
 On Thursday, 14 November 2013 at 21:20:57 UTC, Oleg B wrote:
 [code]
 import std.stdio;

 interface A { void funcA(); }
 class B { final void funcA() { writeln( "B.funcA()" ); } }

 class C: B, A { }

 void main()
 {
    auto c = new C;
    c.funcA();
 }
 [code/]

 $ dmd -run interface.d
 interface.d(6): Error: class interface.C interface function 
 'void funcA()' is not implemented

 if swap A and B
 [code]
 class C: A, B { }
 [code/]

 $ dmd -run interface.d
 interface.d(6): Error: class interface.C base type must be 
 interface, not interface.B

 how to workaround this without change in class B and interface 
 A?
Try interface A { final void funcA(); } class B { final void funcA() { writeln( "B.funcA()" ); } } class C: B, A { }
Oh sorry i mean interface A { void funcA(); } class B : A { final void funcA() { writeln( "B.funcA()" ); } } class C : B { }
Nov 14 2013
parent reply "Oleg B" <code.viator gmail.com> writes:
On Thursday, 14 November 2013 at 21:45:11 UTC, Agustin wrote:
 On Thursday, 14 November 2013 at 21:42:38 UTC, Agustin wrote:
 On Thursday, 14 November 2013 at 21:20:57 UTC, Oleg B wrote:
 [code]
 import std.stdio;

 interface A { void funcA(); }
 class B { final void funcA() { writeln( "B.funcA()" ); } }

 class C: B, A { }

 void main()
 {
   auto c = new C;
   c.funcA();
 }
 [code/]

 $ dmd -run interface.d
 interface.d(6): Error: class interface.C interface function 
 'void funcA()' is not implemented

 if swap A and B
 [code]
 class C: A, B { }
 [code/]

 $ dmd -run interface.d
 interface.d(6): Error: class interface.C base type must be 
 interface, not interface.B

 how to workaround this without change in class B and 
 interface A?
Try interface A { final void funcA(); } class B { final void funcA() { writeln( "B.funcA()" ); } } class C: B, A { }
Oh sorry i mean interface A { void funcA(); } class B : A { final void funcA() { writeln( "B.funcA()" ); } } class C : B { }
we can't change anything in class B
Nov 14 2013
parent Jacob Carlborg <doob me.com> writes:
On 2013-11-14 23:17, Oleg B wrote:

 Oh sorry i mean

 interface A {
   void funcA();
 }

 class B : A {
   final void funcA() { writeln( "B.funcA()" ); }
 }

 class C : B {
 }
we can't change anything in class B
I would have moved "final void funcA()" to a template and mixin it in both B and C, but if you cannot change B that won't work. -- /Jacob Carlborg
Nov 15 2013
prev sibling next sibling parent "Chris Nicholson-Sauls" <ibisbasenji gmail.com> writes:
I had thought the pattern

     alias B.funcA funcA;

was supposed to solve things like this, but it does not work.  
Any reason it couldn't be made to?  Or, for that matter, why 
inherited members are not considered by interfaces in the first 
place?

In related news, if you simply leave A out of C's declaration, 
then do something like:

     A a = cast(A) c;
     a.funcA();

you will get a segfault.  Hooray.
Nov 17 2013
prev sibling parent "Stretto" <uiy12345 gmail.com> writes:
On Thursday, 14 November 2013 at 21:20:57 UTC, Oleg B wrote:
 [code]
 import std.stdio;

 interface A { void funcA(); }
 class B { final void funcA() { writeln( "B.funcA()" ); } }

 class C: B, A { }

 void main()
 {
     auto c = new C;
     c.funcA();
 }
 [code/]

 $ dmd -run interface.d
 interface.d(6): Error: class interface.C interface function 
 'void funcA()' is not implemented

 if swap A and B
 [code]
 class C: A, B { }
 [code/]

 $ dmd -run interface.d
 interface.d(6): Error: class interface.C base type must be 
 interface, not interface.B

 how to workaround this without change in class B and interface 
 A?
This is ambiguous. In one case, from A, you are allowing inheritance and in B you are preventing it. c.funcA will call the interface version unless you cast to B. But it should be an easy fix class C : B, A { void funcA() { ((cast(B))this).funcA(); } } This simply redirects funcA(in C, but using interface A) to funcA in B, which is final.
Nov 17 2013