View mode: basic / threaded / horizontal-split · Log in · Help
February 19, 2012
Template Inheritance
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?
February 19, 2012
Re: Template Inheritance
In the interim, I'm just redefining the template in the base class, but that's a
really annoying hack to have to perform every single time I have to make a new
form of the template.
February 19, 2012
Re: Template Inheritance
Correction: redefining in the *subclass*. Silly me.
February 19, 2012
Re: Template Inheritance
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
February 19, 2012
Re: Template Inheritance
Thanks! I guess I'll just have to live with redefining the functions, do some sort
of interface/mixin thing, or change the class interface. It makes sense that
template functions aren't virtual (how are you supposed to deal with vtables?),
but I wish that at least an alias declaration could work. Maybe if there were some
way to alias the base template and then modify it...

Templates inheriting from templates would be a very interesting way to accomplish
that, but it would be a very strange system...
February 19, 2012
Re: Template Inheritance
I think I got it! This seems to work:

class Derived {
   //Pulls in all the template forms in the base class
   template get(args ...) {
       alias Base.get!args get;
   }

   //Create new versions of get() here.
}
February 19, 2012
Re: Template Inheritance
On Sunday, February 19, 2012 01:17:42 %u wrote:
> Thanks! I guess I'll just have to live with redefining the functions, do
> some sort of interface/mixin thing, or change the class interface. It makes
> sense that template functions aren't virtual (how are you supposed to deal
> with vtables?), but I wish that at least an alias declaration could work.
> Maybe if there were some way to alias the base template and then modify
> it...
> 
> Templates inheriting from templates would be a very interesting way to
> accomplish that, but it would be a very strange system...

aliases only work with actual, concrete functions. So, you could alias an 
specific instantiation of a templated function, but not the templated function 
as a whole. e.g.

alias get!int getInt;

works, but

alias get getVal;

doesn't.

- Jonathan M Davis
February 19, 2012
Re: Template Inheritance
On Sunday, February 19, 2012 01:23:13 %u wrote:
> I think I got it! This seems to work:
> 
> class Derived {
>     //Pulls in all the template forms in the base class
>     template get(args ...) {
>         alias Base.get!args get;
>     }
> 
>     //Create new versions of get() here.
> }

That seems like it could work.

- Jonathan M Davis
February 19, 2012
Re: Template Inheritance
On 02/19/2012 02:23 AM, %u wrote:
> I think I got it! This seems to work:
>
> class Derived {
>      //Pulls in all the template forms in the base class
>      template get(args ...) {
>          alias Base.get!args get;
>      }
>
>      //Create new versions of get() here.
> }

This kills IFTI for the base class templates. It would be nice if it 
would work. Do you want to file an enhancement request? Otherwise I'll 
do it.

This one lets you keep IFTI, but does not forward perfectly for some 
special implicit conversions.
auto get(T...)(T a){return super.get!T(a);}
February 19, 2012
Re: Template Inheritance
On 02/19/2012 01:55 AM, %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).

That is a bug.

> Does anyone know how to get this to work?

There seems to be no perfect way in the current implementation.
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home