digitalmars.D.learn - (this MyType) and interface: Symbol undefined
Hi, please consider following example. I want to acces class B by interface I. Method "work" should print the actual class ("B"). The linker say: Error 42: Symbol Undefined _D3app1I17__T4workTC3app1IZ4workMFZv Is this is missing feature or even a bug? Is there any other way to get the actual class ("B") printed by the inherited method from A? Kind regards André interface I { void work(this MyType)(); } class A : I { void work(this MyType)() { import std.stdio; writeln(MyType.stringof); } } class B : A {} void main() { I i = new B(); i.work(); // Expected output: "B" }
Oct 08 2014
On Wednesday, 8 October 2014 at 13:00:56 UTC, andre wrote:Hi, please consider following example. I want to acces class B by interface I. Method "work" should print the actual class ("B"). The linker say: Error 42: Symbol Undefined _D3app1I17__T4workTC3app1IZ4workMFZv Is this is missing feature or even a bug? Is there any other way to get the actual class ("B") printed by the inherited method from A? Kind regards André interface I { void work(this MyType)(); } class A : I { void work(this MyType)() { import std.stdio; writeln(MyType.stringof); } } class B : A {} void main() { I i = new B(); i.work(); // Expected output: "B" }Interfaces can * declare virtual methods to be implemented by the deriving classes, * implement non-virtual methods themselves. Your `work` is a templated method. Templated methods are non-virtual. So, `work` is expected to be implemented by the interface itself. Principally, the implementation does not need to be in the source. It may be in an object file. That's why the declaration without implementation is allowed, and the linker complains. That doesn't make much sense for templated methods, though. Maybe the compiler should catch it. So, no bug, and not planned to work, as far as I know. There is runtime information about classes: typeid/Typeinfo/classinfo. Depending on what you want to achieve that may be enough. It's enough for printing the class name: interface I { void work(); } class A : I { void work() { import std.stdio; writeln(this.classinfo.name); } } class B : A {} void main() { I i = new B(); i.work(); }
Oct 08 2014