Search code examples
delphi

Why does TList work with item type Pointer, not TObject?


I noticed to my surprise that the RTL class TList (found in unit System.Classes) uses the type Pointer throughout most everything it does (parameter and return types, internal storage).

Why does it not use TObject instead?

I understand that the two are exchangeable – a TObject is a Pointer, and one can be used in place of the other – but why was Pointer chosen? Even if the end result is the same, wouldn't TObject be more easily understood, while Pointer requires understanding of the underlying details? I'm not saying that that is particularly hard, but still, why even this tiny bit of obscurity? What is the benefit of Pointer?


Solution

    • Pointer is the most common denominator for all types, as you can address anything with it: Integer, String, Double, records, objects, classes, interfaces...

    • TObject is the most common denominator for all classes, as every class inherits from this one: TStream, TList, Exception... (see System.Classes.pas).

    The former works always, not just in object oriented programming (OOP). The latter implies always dealing with objects and neglects all the "primitive"/historic types. If we'd be Java then we would have classes of TInteger, TString, TDouble and so on just for the sake of instanciating it to objects.

    So TList isn't limiting its storage to OOP only, but instead to Pascal's full potential. Unbound to the fact that TList can only be used/instanciated by using OOP.