[fpc-devel] sizeof member in class procedure

Marco van de Voort marcov at stack.nl
Wed Aug 15 11:50:06 CEST 2018


In our previous episode, Benito van der Zander said:
> why is sizeof on object/class fields sometimes allowed and sometimes not?
> 
> type TTest = object
>  ? f: integer;
>  ? class procedure test;
> end;
> 
> class procedure TTest.test;
> begin
>  ? writeln(sizeof(f));???????????? // does not compile
>  ? writeln(sizeof(TTest.f));? // compiles
> end;

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.



More information about the fpc-devel mailing list