February 19, 2012
On 2012-02-19 02:07, Jonathan M Davis wrote:
> On Sunday, February 19, 2012 00:55:59 %u wrote:
>> I've been working on porting an old D library to D2, and I'm running into a
>> nasty issue with templates and inheritance. I've got a base class like this:
>>
>> class Reader {
>>      void get(T)(ref T[] buffer);
>> }
>>
>> and a subclass like this:
>>
>> class SubReader {
>>      void get()(SomeClass param);
>> }
>>
>> The problem is that by creating a new form of the template in the subclass,
>> I made the base class's version invisible. If I try to use "alias
>> Reader.get get" like I would do for functions, the compiler complains that
>> the symbols clash (no musical puns intended). Does anyone know how to get
>> this to work?
>
> Template functions are non-virtual. You can't derive from them. If you want
> the derived classes to have the same functions, you must redefine them in the
> derived class.
>
> - Jonathan M Davis

Yeah, but isn't that an overload, or specialization, in the subclass?

-- 
/Jacob Carlborg
February 19, 2012
On Sunday, February 19, 2012 12:34:07 Jacob Carlborg wrote:
> > Template functions are non-virtual. You can't derive from them. If you
> > want
> > the derived classes to have the same functions, you must redefine them in
> > the derived class.
> > 
> > - Jonathan M Davis
> 
> Yeah, but isn't that an overload, or specialization, in the subclass?

Yes. But if you're trying to give the function different behavior in the derived class, what else can you do? Of course, it won't work with polymorphism though (the version that gets called will depend on the reference that you're using), so it may not be such a good idea from that standpoint.

Really, templated functions and class hierarchies don't get along very well.

- Jonathan M Davis
February 22, 2012
That last one looks a lot better than my solution. It's certainly a lot clearer.

One problem I discovered with using templates was that I ended up needing virtual functions, which means that I had to convert the template functions to mixins and just instantiate them for each type (at least there were only two types to handle!) in the base class. The problem I've got now is that if I create versions of get() that take different types in subclasses, I lose access to the superclass's overload set. If I try to use "alias Base.get get", DMD complains that the alias and the functions conflict. It looks like I can override existing overloads but not create new ones. I guess I might have to put a hold on the project until the language gets modified (assuming that actually happens). How would I go about filing an enhancement request?
February 25, 2012
On 02/22/2012 01:13 AM, BLM wrote:
> That last one looks a lot better than my solution. It's certainly a lot clearer.
>
> One problem I discovered with using templates was that I ended up needing virtual
> functions, which means that I had to convert the template functions to mixins and
> just instantiate them for each type (at least there were only two types to
> handle!) in the base class. The problem I've got now is that if I create versions
> of get() that take different types in subclasses, I lose access to the
> superclass's overload set. If I try to use "alias Base.get get", DMD complains
> that the alias and the functions conflict. It looks like I can override existing
> overloads but not create new ones. I guess I might have to put a hold on the
> project until the language gets modified (assuming that actually happens). How
> would I go about filing an enhancement request?

You can post it in the bug tracker and choose severity as 'enhancement':
http://d.puremagic.com/issues/
1 2
Next ›   Last »