December 27, 2010 Re: abstract function templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen kjaeraas | The sense is that I have different, drawable classes/object - for example a simple texture (not clickable) and a button (clickable). When the user clicks the mouse, the obj-param which is defined by using draw!(T = void*)(uint zPos, T obj = null) will be saved in a template-struct which should have the type of obj -> struct myStruct (T). The advantage is, that I can use the obj referenced in the struct without any casting. I just solved the problem by declaring draw() as final-function and used a new value as callback in the final draw()-function - for example: ... private void delegate() drawCallback; public final void draw(T = void*)(uint zPos, T obj = null) { drawCallback(); // ... do additional work } |
December 28, 2010 Re: abstract function templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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 |
December 28, 2010 Re: abstract function templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | Ali Çehreli:
> > But uncomment the override and it fails.
>
> That's because "Templates cannot be used to add [...] virtual functions to classes"
But the error messages given by DMD 2.051 aren't easy to understand for me:
test.d(10): Error: variable test.Bar.draw!(int).draw.this override cannot be applied to variable
test.d(10): Error: variable test.Bar.draw!(int).draw.t override cannot be applied to variable
Bye,
bearophile
|
December 28, 2010 Re: abstract function templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | Thanks, Ali! :) |
December 28, 2010 Re: abstract function templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 12/28/10, bearophile <bearophileHUGS@lycos.com> wrote:
> But the error messages given by DMD 2.051 aren't easy to understand for me:
>
> test.d(10): Error: variable test.Bar.draw!(int).draw.this override cannot be
> applied to variable
> test.d(10): Error: variable test.Bar.draw!(int).draw.t override cannot be
> applied to variable
Agreed. Can you file a bug report?
|
December 28, 2010 Re: abstract function templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | Andrej Mitrovic:
> Agreed. Can you file a bug report?
Sorry, but I don't understand the topic enough yet (I don't know why templated methods can't be virtual), so I leave the bug report to someone else that's able to write something meaningful in the bug report :-)
Bye,
bearophile
|
December 28, 2010 Re: abstract function templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | bearophile wrote: > (I don't know why templated methods can't be virtual), so I leave the bug report to someone else that's able to write something meaningful in the bug report :-) I created a bug report just about the compiler error message: http://d.puremagic.com/issues/show_bug.cgi?id=5387 I think the compiler rejects the code according to spec. Ali |
December 28, 2010 Re: abstract function templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | bearophile <bearophileHUGS@lycos.com> wrote: > (I don't know why templated methods can't be virtual) First of all, they can. But it's a shitload of extra work, and requires that compilation be mixed up with linking. Whenever a templated method is used from any subclass, it has to be generated for the base class, and any and all subclasses that override it. We can't know every subclass until link-time (not even then, given dynamic linking). -- Simen |
Copyright © 1999-2021 by the D Language Foundation