Recent

Author Topic: Record assignment operator  (Read 1182 times)

CandyMan30

  • New Member
  • *
  • Posts: 17
Record assignment operator
« on: April 11, 2021, 06:24:34 pm »
Does FPC support variable length record assignment operator like in Delphi version 10.4?

If not, when would this be expected?

class operator Assign(var Dst:TRec; const Src:TRec);

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Record assignment operator
« Reply #1 on: April 11, 2021, 07:09:33 pm »
With fpc you can simply override the " := " operator.

Bart

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: Record assignment operator
« Reply #2 on: April 11, 2021, 07:35:05 pm »
Not up to date with Delphi, but what is a "variable length record"? I only know fixed length records. Even a record with strings has a fixed length because a string is a pointer and always occupies in the record the size of a pointer (4 or 8).

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Record assignment operator
« Reply #3 on: April 11, 2021, 07:55:29 pm »
Hi!

He means something like this:

Code: Pascal  [Select][+][-]
  1. type Vrec = Record case boolean of
  2.             true : (xb,yb,zb: Byte);
  3.             false: (xi,yi,zi: Integer);
  4.             end;

AFAIK fpc does not care about the different length of the different variants but reserves space for larger case.

Winni

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: Record assignment operator
« Reply #4 on: April 11, 2021, 08:00:27 pm »
The size of a variant record is determined by the size of the longest case, so again a fixed-size record:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. type Vrec = Record case boolean of
  3.             true : (xb,yb,zb: Byte);
  4.             false: (xi,yi,zi: Integer);
  5.             end;
  6. var
  7.   v1, v2: Vrec;
  8. begin
  9.   v1.xb := 1;
  10.   v2.xi := 2;
  11.   WriteLn(SizeOf(v1));   // ---> 12
  12.   WriteLn(SizeOf(v2));   // ---> 12
  13.  
  14.   ReadLn;
  15. end.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Record assignment operator
« Reply #5 on: April 11, 2021, 09:15:22 pm »
Please scroll down a bit to look at the "Assign" operator which apparently exists in Delphi 10.4 now.

I believe that is what was referenced.
https://www.delphipraxis.net/204225-custom-managed-records-coming-delphi-10-4-a.html

OK, so it's relevant to records with managed fields. What does it do that can't be done by overloading FPC's := operator?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Record assignment operator
« Reply #6 on: April 11, 2021, 09:25:58 pm »
Does FPC support variable length record assignment operator like in Delphi version 10.4?

If not, when would this be expected?

class operator Assign(var Dst:TRec; const Src:TRec);

That is not a "variable length record assignment operator" that is simply a part of the available operator overloads for Custom Managed Records.

In FPC this is part of the management operators that were introduced in FPC 3.2.0 (sadly we don't have them in the documentation yet...)

So the corresponding declaration in FPC is this:

Code: Pascal  [Select][+][-]
  1. class operator Copy(constref Src: TRec; var Dst: TRec);

(as can be seen here).

With fpc you can simply override the " := " operator.

Records already have an internal assignment operator thus you can not overload it.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Record assignment operator
« Reply #7 on: April 11, 2021, 09:29:32 pm »
Records already have an internal assignment operator thus you can not overload it.

Thanks for that.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Record assignment operator
« Reply #8 on: April 12, 2021, 09:18:08 am »
It's too bad I didn't know about this Copy operator before when my code from 3.0.4 over to 3.2.0 failed with the inline feature not working as well as it did in 3.0.4

It always pays well to take a look at the New Features page (and the page about changes as well ;) ).

 

TinyPortal © 2005-2018