Developpez.com

Delphi

Choisissez la catégorie, puis la rubrique :

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.

unit Generics.Nullable;

interface

type
  TNullable<T> = record
  private
    FValue: T;
    FIsNil: Boolean;
  public
    class operator Implicit(const Value: T): TNullable<T>;
    class operator Implicit(Value: Pointer): TNullable<T>;
    class operator Implicit(const Value: TNullable<T>): T;

    property IsNil: Boolean read FIsNil;
    property Value: T read FValue;
  end;
			
info For more information about operator overloading, I recommand you read the (French-speaking) tutorial fr La surcharge d'opérateurs sous Delphi 2006 Win32, written by Laurent Dardenne.
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.

uses
  SysUtils;

resourcestring
  sCantConvertNil = 'Cannot convert to nil';
  sOnlyValidValueIsNil = 'The only valid value is nil';

class operator TNullable<T>.Implicit(const Value: T): TNullable<T>;
begin
  Result.FValue := Value;
  Result.FIsNil := False;
end;

class operator TNullable<T>.Implicit(Value: Pointer): TNullable<T>;
begin
  Assert(Value = nil, sOnlyValidValueIsNil);
  Result.FIsNil := True;
end;

class operator TNullable<T>.Implicit(const Value: TNullable<T>): T;
begin
  if Value.IsNil then
    raise EConvertError.Create(sCantConvertNil);

  Result := Value.FValue;
end;
			
This small record can be used with this kind of code:

var
  Value: Integer;
  NullValue: TNullable<Integer>;
begin
  NullValue := 5;
  WriteLn(Integer(NullValue));
  NullValue := nil;
  if NullValue.IsNil then
    WriteLn('nil')
  else
    WriteLn(NullValue.Value);

  NullValue := 10;
  Value := NullValue;
  WriteLn(Value);
end;
			
The exection of which yields:

5
nil
10
			
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() [function.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) [function.include]: 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() [function.include]: Failed opening 'http://sjrd.developpez.com/references.inc' for inclusion (include_path='.:/usr/php53/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 et 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.

Responsables bénévoles de la rubrique Delphi : Gilles Vasseur - Alcatîz -