What's wrong with D's templates?
BCS
none at anon.com
Fri Dec 18 12:18:59 PST 2009
Hello dsimcha,
> Since generics work by basically casting stuff to Object (possibly
> boxing it) and casting back, I wonder if it would be easy to implement
> generics on top of templates through a minimal wrapper.
How about this?
interface Foo { int I(int); }
class CFoo(T) : Foo
{
T t;
int I(int i){ return t.I(i); }
}
class CollectionOfFoos_Base(T)
{
protected:
Foo opIndex_(int i){ ... }
Foo opIndex_(int i, Foo f){ ... }
}
class CollectionOfFoos(T) : CollectionOfFoos_Base
{
public
T opIndex(int i){ return (cast(CFoo!(T))opIndex(i)).t; }
T opIndex(int i, Foo f)
{
auto b = new CFoo!(T);
b.t = f;
return (cast(CFoo!(T))opIndex(i,b)).t;
}
}
Add opDispatch and it might get even better.
More information about the Digitalmars-d
mailing list