Delphi-Oriented Generics, or what a DOG.

The other day I got an email from Embarcadero promoting a blog post that Marco Cantu had written about generics in Delphi, and in particular with regard to generic collections. Now, me, I wrote a book a little while ago (first published 19 years ago!) about algorithms and data structures for Delphi, and I’ve hovering on the fence about redoing it using generics instead of pointers. Problem is, I’ve moved away from Delphi to C# and JavaScript and so would the presence of generics help me to swing back into the fold?

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 (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 T.

OneList := TObjectList<TOne>.Create();
TwoList := TObjectList<TTwo>.Create(); // dupe that list code
ThreeList := TObjectList<TThree>.Create(); // dupe that list code
...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”.

So, nope, I won’t be editing my Delphi book to use generics.

Wrong Way sign

Loading similar posts...   Loading links to posts on similar topics...

1 Response

 avatar
#1 Eber Irigoyen said...
27-Jan-20 3:59 PM

Ouch

Leave a response

Note: some MarkDown is allowed, but HTML is not. Expand to show what's available.

  •  Emphasize with italics: surround word with underscores _emphasis_
  •  Emphasize strongly: surround word with double-asterisks **strong**
  •  Link: surround text with square brackets, url with parentheses [text](url)
  •  Inline code: surround text with backticks `IEnumerable`
  •  Unordered list: start each line with an asterisk, space * an item
  •  Ordered list: start each line with a digit, period, space 1. an item
  •  Insert code block: start each line with four spaces
  •  Insert blockquote: start each line with right-angle-bracket, space > Now is the time...
Preview of response