First of all, here’s Marco’s blog post: Delphi RTL: Generic vs. Traditional Collections. Go read/scan it, I’ll wait.
For me, my immediate thoughts were (1) Delphi has had generics for over 10 years now? Wow. Me, I worked on C#’s generics way back in 2003 when I was at Microsoft, and for me, once you use them you never want to go back. So, more fool me, I should have done more research a few years back.
And then (2), Marco’s comment that “generics in Delphi cause a significant code bloat, because for any data type used by the collection the class methods are replicated, even if almost identical.” Code bloat? Wut? Never seen anything like that in C#. And he never explains further.
So I went on a hunting spree. Ladies and gentlemen, it is a mess.
Suppose you are using the
TObjectList<T> class all over the place for lots of different
T classes. It turns out that the Delphi compiler will replicate/emit the code for the list for every single type
OneList := TObjectList<TOne>.Create(); TwoList := TObjectList<TTwo>.Create(); // dupe that list code, baby! ThreeList := TObjectList<TThree>.Create(); // dupe that list code, baby! ...use the lists and free them...
For another example, if you are using the same type
T in two or more separate units for the object list, the code will still get duplicated. It’s even worse if you use packages: in this case the compiler has no choice but to include All The Things, since it has no way of knowing what would be used by an application using the package. No wonder there’s code bloat.
Or rather, WTF is the linker doing? It should recognize that I’m using the same object list code for different types, and merge them/remove the duplicates. Make some effort at least. But it seems no.
Look, as I said above, it’s been over 10 years since generics were added to Delphi, and I’d have to say that, since this buggy behavior has not been fixed/ameliorated (and my investigations seem to indicate that no real efforts are being expended on this – Marco’s comment being the accepted norm now), it seems that generics were only really added to Delphi to say “hey we’ve got generics too, so no need to switch away from Delphi”. I can’t tell you how utterly (bitterly?) disappointed I am.
So, nope, I won’t be editing my Delphi book to use generics.