views:

277

answers:

2

Is there a way to implement static polymorphism in Delphi 2010?

+3  A: 

From Delphi 2009 on there are generics. Combined with class or interface constraints you can use them to implement static polymophism.

Unfortunately, generics support in Delphi 2009 is very buggy, so you might want to consider using Delphi 2010 (rereading your question, you seem to be using D2010 anyway)

Smasher
But class or interface constraints in generics means that vmt will be used (runtime overhead) and this is still dynamic polymorphism. Isn't it?
valentyn
The runtime overhead should be minimal (1 array lookup I guess), so why does it make a difference after all?
Smasher
Array lookup and procedure call I guess. The same virtual function call overhead. So it is the same question as "why use static polymorphism if there is dynamic polymorphism". I think it makes difference when it repeats huge number of times. Anyway why should I pay for it at runtime when I have compile-time knowledge of which function to call (or call none).
valentyn
Valentyn, nobody's forcing you to use generics. You're free to write an ordinary non-generic function that directly calls your non-virtual methods. You can use overloading to give several such functions the same name.
Rob Kennedy
+6  A: 

Static polymorphism isn't possible in Delphi; generics aren't templates. Other than templates, generics are parsed at declaration time, not at instantiation time. That is why a compiled module (*.dcu) only needs to contain an AST representation of the generic entity, whereas building an AST from an uninstantiated C++ template declaration is nearly impossible.

Moritz Beutel