Re: interface and class inheritance
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.
Re: interface and class inheritance
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.
Re: interface and class inheritance
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
Re: interface and class inheritance
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
Re: interface and class inheritance
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 { }
Re: interface and class inheritance
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 { }
Re: interface and class inheritance
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
interface and class inheritance
[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?