New Delphi 2009 Generics

Finally Generics are a part of Delphi for Win32! This is a big push for CodeGear in order to bring Delphi out of stagnation it has been the last couple of years.

Impressions after working with generics for a week:

  • Supports most of the things you’d expect it to support. After working for 2 years in a .NET/C# environment full of generic code, Delphi for Win32 looks quite familiar.
  • Very little documentation so you have to manually try out until you figure out every little detail.
  • TypeInfo(T) where T would be the generic type works. That is a very big plus because you can figure out the type info (and take appropriate decisions as run-time) of the type you’re working on.
  • This pattern we’re used to will not work:
    type
    PNode = ^TNode;
    
    TNode<T> = record
    Next : PNode;
    Data : T;
    end;
    

    Yeah, you can try to set the <T>’s after PNode type definition and a lot of other tinkering around, it will just not accept it unfortunately. But that is not a big issue as you can obtain this result using a class – described in next points.

  • Unfortunately all generics are kinda only useful in classes. You can’t declare a global generic function, you can’t use linked generic records and so on.
  • There are a lot of restrictions – too many for my taste. For instance if you have a method from a generic class and make a call from it to a function that is only present in the implementation section you will that annoying compile error that you can’t do that. That is actually quite weird. The only way of getting over this is to pre-declare this functions in the interface section of your unit.
  • Too many Internal errors. If you want to develop some interesting generic classes (which would do more than just some basic stuff) be ready to see lots of “Internal error: EXXXX”. the only way around this is to find the offending code and change it to something else 🙂
  • Creating collection classes is hard because (I’ve used generics only in C# before) you don’t have GetHashCode/Compare/Equals (defined per each non-object type). The only way is to use RTTI to get the type and select the appropriate “support” class for each data-type you are operating on.
  • Collections.Generic and Collections.Defaults units are present in the RTL for 2009 but are not documented in the Help system (will be in the nearing Update 1). You can look there for lots of examples. Note that those classes are not what I call – clean 🙂 Also be sure to buy yourself a beer before you get into Collections.Defaults (I’ll see why).
  • IDE is stupid! Yes, there you have it – I said it 🙂 It seems the guys responsable for the compiler did not talk too much to the IDE guys so now you have the editor which shows you lots of “errors” in the code because it doesn’t pick up the syntax correctly.

The current aging RTL and limited Generic collection options pushed me to start my own little project: HelperLib. You can see quite a lot of intersting stuff in HelperLib.TypeSupport and HelperLib.Collections.*

If you’re intersted to write some code drop me an email.