> Unlike Delphi, we have TCompare class and TEquals class, which is used
> for manual interfaces (together with TRawInterface and
TComTypeSizeInterface).
> Additionally (unlike in Delphi which has hidden regular functions for
this in
> implementation section with fake self parameter) TCompare and TEquals can
> be used in custom comparers.

I've just like to point out that with Svens new support for generic
functions, we actually have template like functionality in Free Pascal. You
can now write functions which will verify at compile time the support of a
method or operator for a particular type. This somewhat obviates the need
to register or pass compare or equal functions or interfaces.

For example, you can now write a in a type "TArray<T> = array of T" sorting
implementation:

procedure Sort<T>(var Items: TArray<T>);
var
  A, B: T;
  I: Integer;
begin
  for I := Low(Items) to High(Items) - 1 do
  begin
    A := Items[I];
    B := Items[I + 1];
    if A > B then
    begin
     Items[I] := B;
     Items[I + 1] := A;
    end;
  end;
end;

Obviously this is a terribly inefficient sorting algorithm, but it
demonstrates the feature clearly. The condition "if A > B then" will be
cause the compiler to verify at compile time whether or not the T being
used support comparison through the "<" operator. The same kind of support
extends to the "=" operator as well.

If you using symbols are unclear, the above routine could be altered to use
a CompareTo method. For example:

  if A.CompareTo(B) < 0 then

This would then cause the compiler to check the type B for a CompareTo
function with the appropriate signature. This can then be paired with type
helpers to add CompareTo methods to any type including the intrinsic ones.

Just so you know ...
_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply via email to