Understanding Templates: why can't anybody do it?
Nick Sabalausky
a at a.a
Sat Mar 17 17:37:35 PDT 2012
"H. S. Teoh" <hsteoh at quickfur.ath.cx> wrote in message
news:mailman.834.1332023905.4860.digitalmars-d at puremagic.com...
>
> (Not to mention, D templates can do some stuff that no OO can hope to
> attain. But it goes both ways. Templates can't do runtime polymorphism
> either.)
>
Combined with compile-time reflection, I'm sure they could be used to create
a runtime polymorphism tool, even multiple dispatch. Like I mentioned
recently in a seperate thread, you'd just need to use templates/ctfe to
automate/genericize this:
void singleDispatch(Object o)
{
if(auto derived = cast(DerivedClassA)o)
derived.foo();
else if(auto derived = cast(DerivedClassB)o)
derived.foo();
else if(auto derived = cast(DerivedClassC)o)
derived.foo();
//etc...
}
Although I guess *technically* you're still relying on OO polymorphism (in
the form of downcasting) to achieve this. OTOH, you could still base it on
Variant or Algebraic instead of (or in addition to) Object. Then you'd have
built runtime polymorphism out of templates (among other things) without
relying on OO.
More information about the Digitalmars-d
mailing list