Warning: filemtime(): stat failed for /home/developpez/www/developpez-com/upload/sjrdhttp://sjrd.developpez.com/stylesheet.css in /home/developpez/www/developpez-com/template/entete.php on line 241
IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

Generics with Delphi 2009 Win32

With, as a bonus, anonymous routines and routine references

Date de publication : November 13th, 2008


I. Introduction
I-A. Prerequisite
I-B. What are generics?


I. Introduction


I-A. Prerequisite

This tutorial was not written for the beginner in Delphi, or for a programmer who has never practised object oriented programming. It requires a good knowledge of the Delphi language, and non-negligible knowledge of OOP.

As for generics, no prior knowledge is needed for a good understanding of this tutorial. But, seeing there are already many existing articles on generics in other languages, I will not cover in details questions of matters like "How to use generics well?". The next section will introduce briefly the reader to the subject, but if you do not already know what generics are, the best understanding will come with the rest of the tutorial, with examples.

Here are some (French-speaking) articles covering the subject of generics in other languages/platforms. The concepts are always the same, and a fair amount of undertaking is as well.


I-B. What are generics?

Generics are sometimes called generic parameters, a name which allows to introduce them somewhat better. Unlike a function parameter (argument), which has a value, a generic parameter is a type. And it parameterize a class, an interface, a record, or, less frequently, a method.

In order to ease the understanding immediately, here is an extract of the TList<T> class, which you may find in the Generics.Collections.pas unit.

info Yes, it is possible to have dots in a unit name (except for that of the .pas, of course). This has nothing to do with the concept of namespace in .NET, neither the one of package in Java. It has simply the same meaning as a _ on that level: it is part of the name.

type
  TList<T> = class(TEnumerable<T>)
    // ...
  public
    // ...
    
    function Add(const Value: T): Integer;
    procedure Insert(Index: Integer; const Value: T);

    function Remove(const Value: T): Integer;
    procedure Delete(Index: Integer);
    procedure DeleteRange(AIndex, ACount: Integer);
    function Extract(const Value: T): T;
    
    procedure Clear;
    
    property Count: Integer read FCount write SetCount;
    property Items[Index: Integer]: T read GetItem write SetItem; default;
  end;
				
You may readily see the weirdness of the type T. But is it really a type? No, it is a generic parameter. With this class at my disposal, if I need a list of Integers, I will use TList<Integer>, instead of a "simple" TList, along with many necessary casts in the code!

Generics thus allow, somehow, to declare a set of (potential) classes with a single code, or -more formally- a class template, but with one (or several) parameterized types, which you may change at will. Each time a new actual type is instanciated, you kind of write an entire new class, following its template, and, thus, make real one of those potential classes.

As I said before, I will not spread on the why and the how of generics. I will immediately go on with their daily use. If you think you still haven't got a clue on what I say, do not worry: everything is going to become clearer in a minute. You may also read the (French-speaking) tutorial fr Les génériques sous Delphi .NET written by Laurent Dardenne.

 

Warning: include(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in /home/developpez/www/developpez-com/upload/sjrd/delphi/tutoriel/generics/index.php on line 41

Warning: include(http://sjrd.developpez.com/references.inc): failed to open stream: no suitable wrapper could be found in /home/developpez/www/developpez-com/upload/sjrd/delphi/tutoriel/generics/index.php on line 41

Warning: include(): Failed opening 'http://sjrd.developpez.com/references.inc' for inclusion (include_path='.:/opt/php56/lib/php') in /home/developpez/www/developpez-com/upload/sjrd/delphi/tutoriel/generics/index.php on line 41

Valid XHTML 1.0 TransitionalValid CSS!

Copyright © 2008-2009 Sébastien Doeraene. Aucune reproduction, même partielle, ne peut être faite de ce site ni de l'ensemble de son contenu : textes, documents, images, etc. sans l'autorisation expresse de l'auteur. Sinon vous encourez selon la loi jusqu'à trois ans de prison et jusqu'à 300 000 € de dommages et intérêts.