[fpc-pascal] fgl.TFPGMap

Alex Shishkin alexvins at mail.ru
Thu Jan 20 21:42:28 CET 2011


20.01.2011 22:18, Honza пишет:
> 2011/1/20 Alex Shishkin<alexvins at mail.ru>:
>> Following code causes error "Operator is not overloaded: "TObject"<
>> "TObject""
>> How to make it work?
>>
>> program generictest2;
>> {$mode objfpc}{$H+}
>> uses
>>   fgl;
>> operator<  (a: TObject; b: TObject): boolean;
>> begin
>>   result := PtrInt(a)<  PtrInt(b);
>> end;
>> type
>>   TMyMap = specialize TFPGMap<TObject, TObject>;
>> begin
>> end.
> AFAIK TFPGMap can be specialized only with types which have the
> relational operators predefined by the compiler. See also:
> http://bugs.freepascal.org/view.php?id=15480
As I understand problem is in the compiler. It cant use overloaded 
operator if it was defined after generic.
BTW this code compiles successfully.
program generictest2;
{$mode objfpc}{$H+}
operator > (a: TObject; b: TObject): boolean;
begin
   result := PtrInt(a) > PtrInt(b);
end;
type
   generic TComparator <T> = class
     class function Compare(a,b:T): Integer;
   end;
class function TComparator.Compare(a, b: T): Integer;
begin
   if a>b then
     Result := 1
   else if b>a then
     Result := -1
   else
     Result :=0;
end;

type
   TObjectComparator = specialize TComparator<TObject>;

begin

end.
Data can be any type  but Key only "native comparable". If drop 
TFPGMap.KeyCompare along with DataCompare leaving only custom comparison 
it will work with any Key type.
> You can take a look on this container lib, which might be helpful for your task:
> http://code.google.com/p/fprb/wiki/heContnrs
Good lib, but Lazarus codetools does not support  nested classes as for now.



More information about the fpc-pascal mailing list