Generics in delphi

The strong type checking provided by Delphi is useful for improving the correctness
Of the code, a topic I tend to stress in my introductory books.
Strong type checking, though, can also be a nuisance, as you might rather
Have a procedure or a class that can act on different data types. This issue is
Addressed by a new feature of the Object Pascal language, recently added to
Similar languages like C# and Java, called generics. This is what I wrote in
1994 in a book about C++70:
Now you can declare a class without specifying the type of one or
More data members: this operation can be delayed until an object
Of that class is actually declared. Similarly, you can define a function
Without specifying the type of one or more of its parameters
Until the function is called.
Now, 14 years later, this feature is getting into Object Pascal. You can guess
I’m somewhat excited to have generics (they are called templates in C++) in
Delphi, although I have to say I have witnessed distinct overuse of this feature
That I didn’t fully understand at the time. I doubt generics will be
Overused in Delphi, quite the contrary: there is the risk that a very significant
Language upgrade gets almost unnoticed. This chapter will try to delve
Into the topic, showing you the value of generics in Delphi and how they can
Be applied even to standard visual programming.

Generic Key-Value Pairs
As a first example of a generic class, I’ve implemented a key-value pair data
Structure. The first code snippet below shows the data structure written in a
Traditional fashion, with an object used to hold the value:
Type
TKeyValue = class
Private
FKey: string;
FValue: TObject;
Procedure SetKey(const Value: string);
Procedure SetValue(const Value: TObject);
Public
Property Key: string read FKey write

SetKey;
Property Value: TObject read FValue write SetValue;
End;
To use this class you can create an object, set its key and value, and use it, as
In the following snippets of various methods of the main form of the Key-
ValueClassic example:
// FormCreate
Kv := TKeyValue. Create;
// Button1Click
Kv. Key := ‘mykey’;
Kv. Value := Sender;
// Button2Click
Kv. Value := self; // the form
// Button3Click
ShowMessage(‘[‘ + kv. Key +’,’ + kv. Value. ClassName + ‘]’);

Generics make it possible to use a much broader definition for the value, but
That’s not the key point. What’s totally different, as we’ll see, is that once
You’ve instantiated the key-value generic class, it becomes a specific class,
Tied to a given data type. This makes your code type safer, but I’m getting
Ahead of myself. Let’s start with the syntax used to define the generic class:
Type
TKeyValue = class
Private
FKey: string;
FValue: T;
Procedure SetKey(const Value: string);
Procedure SetValue(const Value: T);
Public
Property Key: string read FKey write SetKey;
Property Value: T read FValue write SetValue;
End;
In this class definition, there is one unspecified type, indicated by the placeholder
T71. The symbol T is frequently used by convention, but as far as the
Compiler is concerned you can use just any symbol you like. Using T generally
Makes the code more readable when the generic class uses only one
Parametric type; in case the class needs multiple parametric type it is common
To name them according to their actual role, rather than using a
Sequence of letters (T, U, V) as it happened in C++ during the early days.
The generic TKeyValue class uses the unspecified type as the type of one


1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)



Generics in delphi