// *** before: ***
// a callback was a method pointer: it needed an object, a method with
// exactly the right signature, and it was written far from the call
type
TCompareEvent = function(const A, B: string): Integer of object;
TSorter = class
function ByLength(const A, B: string): Integer;
end;
function TSorter.ByLength(const A, B: string): Integer;
begin
Result := Length(A) - Length(B);
end;
// ...
Sort(List, Sorter.ByLength); // the local context could not be reached
// *** in version 2009: ***
type
TCompare = reference to function(const A, B: string): Integer;
procedure Sort(const Items: TStrings; const Compare: TCompare);
begin
// ...
end;
var
Limit: Integer;
begin
Limit := 3;
// the body is written where it is used, and it captures Limit
Sort(List,
function(const A, B: string): Integer
begin
if Length(A) > Limit then
Result := 1
else
Result := Length(A) - Length(B);
end);
end;