|
Generics with Delphi 2009 Win32
With, as a bonus, anonymous routines and routine references
Date de publication : November 13th, 2008 IV. Design of a generic record IV. Design of a generic record
Let us have a look at something else, and let us develop the simple yet useful record TNullable<T>. Its goal
is to have either a value of type T, either nil. It is highly likely that you have already needed such a
type, for example to represent the NULL value of databases.
This record will contain two fields: FValue of type T, and FIsNil of type Boolean. Two
properties are provided for read access to those fields. We will use only implicit convertion operators to build values of
this type.
So, it is a immutable type (whose state cannot be changed once created).
The implementation of three convertion operators is quite easy. The second one (the one with a Pointer parameter)
is provided to allow the assignment := nil.
This small record can be used with this kind of code:
The exection of which yields:
Understood it all? That is great, because I have not given a single piece of explanation. Proof is given that generics are
as easy as pie :-).
The complete source code of Generics.Nullable is available for download
at the end of the tutorial.
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 |
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.