Re: Template Inheritance
On Sunday, 31 July 2016 at 03:04:27 UTC, Gorge Jingale wrote: I like to build structures using template mixins because one can pick and choose functionality at compile time, but still have a relationship between different types. It would be really nice if one could sort of test if a template mixin was "mixed" in(or ideally, struct S : SomeTemplate (template inheritance)). While one can overcome a lot of the issues by doing static checking(e.g., does IsSomeTemplate compile or other introspective like checks), it is a bit messy and clutters up the code. Does anyone do stuff like this and have a nice elegant way? I suppose you could have the template mixin define a unique key ( kinda like the current(?) std.random's isUniformRandom trick) for that mixin and then do is(hasMember!(S,someUniqueTemplateMixinName))
Re: Template Inheritance
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/
Re: Template Inheritance
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?
Re: Template Inheritance
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
Re: Template Inheritance
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
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.
Re: Template Inheritance
Correction: redefining in the *subclass*. Silly me.
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
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...
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. }
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
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
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.