Recent

Author Topic: Record assignment operator  (Read 479 times)

CandyMan30

  • Newbie
  • Posts: 3
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: 4261
    • 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

jamie

  • Hero Member
  • *****
  • Posts: 4468
Re: Record assignment operator
« Reply #2 on: April 11, 2021, 07:15:24 pm »
its possible that maybe the := wants to be overridden to handle some special processing of moving one over to the other.

That would really throw a monkey wrench in the process but if that is what they want ,,,,

The only true wisdom is knowing you know nothing

wp

  • Hero Member
  • *****
  • Posts: 8410
Re: Record assignment operator
« Reply #3 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).
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

winni

  • Hero Member
  • *****
  • Posts: 2356
Re: Record assignment operator
« Reply #4 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: 8410
Re: Record assignment operator
« Reply #5 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.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

jamie

  • Hero Member
  • *****
  • Posts: 4468
Re: Record assignment operator
« Reply #6 on: April 11, 2021, 08:54:46 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
The only true wisdom is knowing you know nothing

MarkMLl

  • Hero Member
  • *****
  • Posts: 2559
Re: Record assignment operator
« Reply #7 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
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and 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: 2985
  • Compiler Developer
Re: Record assignment operator
« Reply #8 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: 2559
Re: Record assignment operator
« Reply #9 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
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

jamie

  • Hero Member
  • *****
  • Posts: 4468
Re: Record assignment operator
« Reply #10 on: April 11, 2021, 09:58:24 pm »
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

 I could of adapted a record with a class within knowing the "Copy" operator existed!

I just tried that with 3.2.0 with the adaption and it yielded the results I was looking for when using the INLINE in 3.0.4 to gain a reference back to the destination of a class pointer.

 Oh well, too late now since I moved that and a couple other large projects over to Delphi , I am not moving them back..

 Carryon..


The only true wisdom is knowing you know nothing

PascalDragon

  • Hero Member
  • *****
  • Posts: 2985
  • Compiler Developer
Re: Record assignment operator
« Reply #11 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