[fpc-devel] Generics.Collections as package for Lazarus or package for FPC RTL

Anthony Walter sysrpl at gmail.com
Wed Jan 27 21:01:06 CET 2016

> Unlike Delphi, we have TCompare class and TEquals class, which is used
> for manual interfaces (together with TRawInterface and
> 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

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

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 ...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20160127/47aec6c3/attachment.html>

More information about the fpc-devel mailing list