[fpc-devel] should have been [fpc-pascal] why can't we define class operator for old fashion object type

Marco van de Voort marcov at stack.nl
Wed Aug 15 14:10:25 CEST 2018


In our previous episode, Marco van de Voort said:

I accidentally replied the below post to the wrong thread, probably because they were both
about operator definitions. Anyway the post was about the "why can't we
define class operator for old fashion object type" thread, and how to
customize generics.* classes in general:


> For custom types that don't have a predefined IComparer<> instance, afaik
> you can define it yourself and set it in the TDictionary<> type.
> 
> Just search for I/Tcomparer<> in the generics.* sources. 
> 
> I do have some example, but that is for the generic sort, not the
> dictionary:
> 
> program gensort2;
> 
> {$APPTYPE CONSOLE}
> 
> {$R *.res}
> 
> uses
>   System.SysUtils,generics.collections,generics.defaults;
> 
> type
>     TSomeRecord = record
>                     angle:single;
>                     angleasstring:string;
>     end;
> type
> 
>   TSomeRecordComparer1 = class(TComparer<TSomeRecord>)
>   public
>     function Compare(const Left, Right: TSomeRecord): Integer; override;
>   end;
> 
> function TSomeRecordComparer1.Compare(const Left, Right: TSomeRecord):
> Integer;
> begin
>   Result := round(left.angle - right.angle);
> end;
> 
> var  some:array of TSomeRecord;
>      i : integer;
>      cmp : TSomeRecordComparer1;
> begin
>   cmp:=TSomeRecordComparer1.Create;
>   setlength(some,6);
>   some[0].angle:=100; some[1].angle:=25; some[2].angle:=270;
>   some[3].angle:=90; some[4].angle:=15; some[5].angle:=300;
>   for i := 0 to length(some)-1 do
>     some[i].angleasstring:=FloatToStrF(some[i].angle,fffixed,8,4);
> //  tarray.sort<tsomerecord>(some,cmp,3,3);  // partial sort
>   tarray.sort<tsomerecord>(some,cmp);
>   for i := 0 to length(some)-1 do
>     writeln(some[i].angle:10:5,' ',some[i].angleasstring);
>   readln;
> end.
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
> 



More information about the fpc-devel mailing list