Andrej Mitrovic wrote:
> I think this is relevant:
> http://www.digitalmars.com/d/2.0/template.html : "Limitations":
>
> Templates cannot be used to add non-static members or virtual
> functions to classes.
> Templates cannot add functions to interfaces.
>
> But I'm a little confused as to how it all works out. This will work:
>
> import std.stdio;
>
> class Foo
> {
>     void draw(T)(T t) { writeln("Foo"); };
> }
>
> class Bar : Foo
> {
>     /* override */ void draw(T)(T t) { writeln("Bar"); };
> }

Bar.draw does not override, but "hide" Foo.draw. They are unrelated functions.

> void main()
> {
>     Bar bar = new Bar();
>     bar.draw(1); // "Bar"

Bar.draw!int is called. No virtual dispatch is involved, as Foo.draw!int doesn't even exist.

>     (cast(Foo)bar).draw(1); // "Foo"

Now Foo.draw!int is instantiated (at compile time) an is called. There is no Bar.draw!int instantiated at all.

> }
>
> But uncomment the override and it fails.

That's because "Templates cannot be used to add [...] virtual functions to classes"

Ali

Reply via email to