digitalmars.D.learn - interface and class inheritance
- Oleg B (22/22) Nov 14 2013 [code]
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (23/45) Nov 14 2013 One way is to expose B through 'alias this' (not by inheritance) but it
- Agustin (9/32) Nov 14 2013 Try
- Agustin (10/49) Nov 14 2013 Oh sorry i mean
- Oleg B (2/54) Nov 14 2013 we can't change anything in class B
- Jacob Carlborg (5/18) Nov 15 2013 I would have moved "final void funcA()" to a template and mixin it in
- Chris Nicholson-Sauls (11/11) Nov 17 2013 I had thought the pattern
- Stretto (11/34) Nov 17 2013 This is ambiguous. In one case, from A, you are allowing
[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
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
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
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:Oh sorry i mean interface A { void funcA(); } class B : A { final void funcA() { writeln( "B.funcA()" ); } } class C : B { }[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
On Thursday, 14 November 2013 at 21:45:11 UTC, Agustin wrote:On Thursday, 14 November 2013 at 21:42:38 UTC, Agustin wrote:we can't change anything in class BOn Thursday, 14 November 2013 at 21:20:57 UTC, Oleg B wrote:Oh sorry i mean interface A { void funcA(); } class B : A { final void funcA() { writeln( "B.funcA()" ); } } class C : B { }[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
On 2013-11-14 23:17, Oleg B wrote: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 CarlborgOh 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 15 2013
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
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