Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2018-08

sizeof member in class procedure

4 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Benito van der Zander

Hi,

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;

---

type TTest = class
   f: integer;
   class procedure test;
end;
class procedure TTest.test;
begin
   writeln(sizeof(f));             // does not compile
   writeln(sizeof(TTest.f));  // does not compile
end;

surely that should all be equal to sizeof(integer)

Bye,

Benito

Reply by email

Marco van de Voort
to Benito van der Zander

In our previous episode, Benito van der Zander said:

Show quoted text 12 lines
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.

Reply by email

Marco van de Voort
to Marco van de Voort

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:

Show quoted text 55 lines
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 - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply by email

Sven Barth
to Benito van der Zander: why is sizeof on object/class fields sometimes allowed and sometimes not? type TTest =…
Benito van der Zander wrote 29 lines
Hi, 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; --- type TTest = class   f: integer;   class procedure test; end; class procedure TTest.test; begin   writeln(sizeof(f));             // does not compile   writeln(sizeof(TTest.f));  // does not compile end; surely that should all be equal to sizeof(integer)

Interestingly Delphi does not allow it either (though it fails with two different errors for the two variants (note: I only tested the class one)), so its behavior is the same as in FPC there. I personally would say that it should be allowed however... *shrugs*

Regards,
Sven

Reply by email