8.5.2 Record type restrictions

In Delphi mode, the record type restrictions will also allow the use of simple types:

Type
  TList<_T : record> = class(TObject)
  public
    Type TCompareFunc = function(const Item1, Item2: _T): Integer;
  Public
    data : _T;
    procedure Add(item: _T);
    procedure Sort(compare: TCompareFunc);
 end;

TIntList = TList<Integer>;

The restriction is enforced when specializing the type. That means that the mode active when specializing a type determines whether a simple type can be used or not: if the restriction to record was compiled using ObjFPC mode, code written in Delphi mode can specialize it with a simple type anyway.

for example:

unit tg;

interface

{$mode objfpc}

Type
  generic TList<_T : record> = class(TObject)
  public
    Type TCompareFunc = function(const Item1, Item2: _T): Integer;
  Public
    data : _T;
    procedure Add(item: _T);
    procedure Sort(compare: TCompareFunc);
 end;

implementation

generic procedure TList<_T>.Add(item: _T);

begin

end;

generic  procedure TList<_T>.Sort(compare: TCompareFunc);

begin
end;

end.

can be used in {$MODE Delphi} for:

{$mode delphi}
uses tg;

Type
  TIntList = TList<Integer>;
begin
end.