Link: ftp://ftp-developpez.com/sjrd/tutoriels/delphi-generics/delphi-generics.pdf
Just found a wonderful and deep look into generics, anonymous routines and routine references here . Written by Sébastien Doeraene.
Generics were introduced in Delphi 2009, and I'm a big fan of them, as they help me do less type casting on TObjectList's items for example.
Want to know how sorting a TObjectList<TWhatever> works? Here's how it works:
type
TDBObject = class
Name: String;
end;
TDBObjectList = TObjectList<TDBObject>;
TDBObjectComparer = class(TComparer<TDBObject>)
function Compare(const Left, Right: TDBObject): Integer; override;
end;
procedure TMainform.btnOkClick(Sender: TObject);
var
o: TDBObject;
begin
Result := TDBObjectList.Create(TDBObjectComparer.Create);
o := TDBObject.Create;
o.Name := 'foo';
Result.Add(o);
o := TDBObject.Create;
o.Name := 'bar';
Result.Add(o);
end;
function TDBObjectComparer.Compare(const Left, Right: TDBObject): Integer;
begin
Result := CompareText(Left.Name, Right.Name);
end;






