www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: Limited member function templates?

Yigal Chripun Wrote:

 Christopher Wright wrote:
 Janice Caron wrote:
 On 11/10/07, Janice Caron <caron800 googlemail.com> wrote:
 On 11/10/07, Bill Baxter <dnewsgroup billbaxter.com> wrote:
 The only thing you can't have is _virtual_ member function
 templates. Is making them virtual what you are talking about?

the "final" keyword). So obviously, yes.

When we get the common calling convention whereby f(x) and x.f() are interchangable, then adding non-polymorphic template functions will be a piece of cake. Just declare as f(T)(X x, T t) { ... } and call as x.f(t). Maybe that will be enough. But yeah - I intended to mean "normal" functions. Polymorphic. Virtual.

I was just about to feature-request virtual templated member functions. I can't mock them, which is one of two major problems in my mocks library. You also can't have a templated method in an interface: interface Collection (T) { void addAll(U : T) (Collection!(U) toAdd); // fail } Which leads to annoyances: "why can't I add all my Bicycles to my Vehicles collection?"

I may be mistaken, but isn't it enough to use the following: ---- interface Collection (T) { void addAll(T) (Collection!(T) toAdd); } ---- a bicycle should conform to a is-a relationship (a bicycle is a vehicle). so you can in fact add them all due to inheritance. or am I missing something here?

A Collection!(Bicycle) is not a Collection!(Vehicle). You cannot add arbitrary Vehicles to a Collection!(Bicycle), nor can you read arbitrary elements of a Collection!(Vehicle) as Bicycles. You can't cast between the two. So let's say I have Set!(int) called integers and a Set!(long) called longs. In order to add all of the set integers to the set longs, what do I have to do? My fictional Set class defines: void add(T)(T element); void addAll(T) (Set!(T) other); I have to do: foreach (i; integers) longs.add(i);
 templates are only useful at compile time. in run time we need to rely 
 on polymorphism (inheritance).

But Set!(int) doesn't inherit from Set!(long). Set!(Object) and Set!(MyClass) are entirely unrelated. There's no polymorphism here.
 Maybe you could give a better example for the case when you need to have 
 template (virtual) member functions?

I'm not sure I ever *need* them, but they'd be quite convenient. Of course, for me, I want them for my mocks library: it's a large collection of final methods, and I don't currently have a way of mocking final methods.
Nov 10 2007