In the previous example we have seen how you can define and use a generic class in Delphi, one of the most far reaching extensions to the Object Pascal language since Delphi 3 introduced interfaces. I decided to introduce the feature with an example before delving into the technicalities, which are quite complex and very important at the same time. After covering generics from a language perspective we'll get back to more examples, including the use and definition of generic container classes, one of the main reasons this techniques was added to the language.
We have seen that when you define a class in Delphi 2009 you can now add an extra "parameter" within angle brackets to hold the place of a type to be provided later: type
TMyClass<T> = class end;
The generic type can be used as the type of a field (as I did in the previous example), as the type of a property, as the type of a parameter or return value of a function, and more. Notice that it is not compulsory to use the type for a local field (or array), as there are cases in which the generic type is used only as a result, a parameter, or is not used in the declaration of the class, but only in the definition of some of its methods.
This form of extended or generic type declaration is not only available for classes but also for records (that, in case you didn't notice, in the most recent versions of Delphi can also have methods, properties, and overloaded operators). You cannot declare a generic global function, unlike C++, but you can declare a generic class with a single class method, which is almost the same thing and doesn't clutter the global name space.
A generic class can also have multiple parameterized types, as in following case in which you can specify an input parameter and a return value of a different type for a method: type
TPWGeneric<TInput,TReturn> = class publ ic function AnyFunction (Value: TInput): TReturn; end;
The implementation of generics in Delphi, like in other static languages is not based on a runtime framework. It is handled by the compiler and the linker, leaving almost nothing to the runtime mechanism. Unlike virtual function calls, which are bound at runtime, template methods are generated once for each template type you instantiate, and are generated at compile time! We'll see the possible drawbacks of this approach, but on the positive side it implies that generic classes are as efficient as plain classes, or even more efficient as the need for runtime checks is reduced. Before we look at some of the internals, though, let me focus on some very significant rules which break the traditional Pascal language type compatibility rules.
Was this article helpful?
What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.