Page 7 - FREE_TRIAL6Months
P. 7

AN INTRODUCTION TO GENERICS CONTINUATION 2 PAGE 1/5
BY MICHAËL VAN CANNEYT

starter               &expert                                    THE SOLUTION: GENERICS

                             Delphi and Lazarus If we want to reduce the number of functions that

ABSTRACT Generics have been available in Free                   we must implement, then basically - what is
Pascal and in Delphi for quite some time now and are
increasingly used thoughout the VCL. In this article,           required - is a function in which the type of the
we take a closer look at this language construct.               parameters and return value is a parameter
                                                                itself, so it can be specified at a later time, and
                                                                let the compiler generate the function for us,

               INTRODUCTION:                                    with the type we specify.
               THE PROBLEM
                                                                Something like

                             Object Pascal is a strongly typed  Function Min(A,B : T) : T;
                      language. This is one of the good
            reasons for using Object Pascal, since it           begin
ensures that if the code compiles, it will not contain
too many obvious mistakes that can happen in                    If A<B then
languages with e.g. dynamic typing, such as passing
a string or an object to a function that expects an             Result:=A
integer. The compiler will catch such an error at
                                                                else

                                                                Result:=B;

                                                                end;              //Where T is unknown.

compile time. In dynamically typed languages, such              This requirement, to be able to specify the type of
an error will only be detected when                             some fields or function parameters later on,
running the code. It also presents a drawback.                  extends to structures:
In case you want to have a structure or function that

needs to be implemented for several types, then you TList from the classes unit (or TCollection) is

need to implement it separately for each type,                  a classical example.
even if the actual code for the function looks 100%             The TList type can be used with pointers.
the same. For instance:                                         Using some typecasting, it is possible to make

                                                                descendents that accept objects: given a class

For instance:                                                   TMyClass one can make a TMyClassList
                                                                descendent of TList with a default array

Function Min(A,B : Integer) : Integer;                          property that has type TMyClass.
begin                                                           Some overloads are needed with typecasts,
                                                                but it works.
 If A<B then                                                    With GENERICS, it is possible to define a list
   Result:=A
                                                                class, in which the type of the elements is not
 else                                                           yet determined, but which can be filled in later.
   Result:=B;
                                                                The statement where the actual type is
end;

                                                                determined, is called instantiating in Delphi,

This function can be used with an integer type                  or specializing in Free Pascal.

(in fact, all integer-like types such as byte, word             Roughly, this is something like:
etc.). But it cannot be used with a string.

In Javascript, the function                                     TList<T> = Class

function min (a,b) {                                             Property Items [Aindex : Integer] : T Read GetA;
   if (a<b) {                                                   end;

   return a;                                                    Here, T would be a placeholder for a type.
} else {                                                        Generic types (and functions in FPC) are the

   return b;

} solution to this problem. Generic types can be

} made using records (with or without methods),

                                                                classes and arrays and procedural types.

can be used with all basic types (all types for                 In Free Pascal, it is also possible to create actual
which < is defined); you need to write it only                  generic functions or procedures.
once.                                                           The compiler implementation of generics in
In Object Pascal, you need to write the function                Free Pascal and Delphi is presumably quite
for each type that you wish to use it with, the                 different, which means that some things
overloading feature of the language takes care of               which are possible in Free Pascal, will not
selecting the right version when it is used.                    be possible in Delphi.

When implementing the above function                            Since Free Pascal aims to be Delphi compatible, the

Min(), even though the code inside the                          opposite should not be the case

function will look 100% the same, the function                  (barring bugs in Free Pascal, obviously).
must be written for each type separately.

10 Issue Nr 8 2016 BLAISE PASCAL MAGAZINE
   2   3   4   5   6   7   8   9   10   11   12