Recent

Author Topic: [Solved]Impossible to overload assignment of equal type: How to fix that?  (Read 2109 times)

jamie

  • Hero Member
  • *****
  • Posts: 6735
I have a Record that maintains X,Y:Integer; and some other items .

I don't want to use the generic := because it will copy all of the record which I don't want.

I tried to implement the := operator so that I can simply assign the X,Y from another record but not the rest of the contents of the source record.

 Translated from a C/C++ app, where it obviously does work but FPC isn't allowing me to do the same.

 The rest of the operators work ok {+,-,= etc}

 So, if I am able to get the "=" to work, why can't I get the ":=" work, too?

 I understand to some degree this may not make sense to allow an operator like this since the compiler would simply assign one from the other, but in this case, its only some of it that gets assigned.

Is there a switch that I don't know about?

« Last Edit: July 14, 2024, 03:34:21 pm by jamie »
The only true wisdom is knowing you know nothing

MarkMLl

  • Hero Member
  • *****
  • Posts: 8041
Re: Impossible to overload assignment of equal type: How to fix that?
« Reply #1 on: July 13, 2024, 10:53:41 pm »
Interesting. Does that also apply if the assignands are scalars, i.e. 32-bit or smaller, and specifically not composites such as a record?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Coldzer0

  • Jr. Member
  • **
  • Posts: 53
Re: Impossible to overload assignment of equal type: How to fix that?
« Reply #2 on: July 14, 2024, 03:22:11 am »
I have a Record that maintains X,Y:Integer; and some other items .

I don't want to use the generic := because it will copy all of the record which I don't want.

I tried to implement the := operator so that I can simply assign the X,Y from another record but not the rest of the contents of the source record.

 Translated from a C/C++ app, where it obviously does work but FPC isn't allowing me to do the same.

 The rest of the operators work ok {+,-,= etc}

 So, if I am able to get the "=" to work, why can't I get the ":=" work, too?

 I understand to some degree this may not make sense to allow an operator like this since the compiler would simply assign one from the other, but in this case, its only some of it that gets assigned.

Is there a switch that I don't know about?

If I understand correctly, you want to copy x and y from one record to another.
Or Can you show an example of what you are trying to do; maybe I can help.

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: Impossible to overload assignment of equal type: How to fix that?
« Reply #3 on: July 14, 2024, 04:13:23 am »
hand written here as best as I can explain it.

Code: Pascal  [Select][+][-]
  1. Type
  2.   TMyRecord = Record
  3.     X,Y:Integer;
  4.     SomeotherFields:Integer; //That I don't want to copy.
  5.     ....
  6.     class operator :=(A:TMyRecord; B:TMyRecord):TMyRecord;
  7.   End;
  8.  
  9. Implementation
  10. Class Operator TMyRecord.:=(A:TmyREcord; B:TMyRecord):TMyRecord
  11. Begin
  12.   A.X := B.X;
  13.   A.Y := B.Y;
  14.  Result := A;
  15. End;
  16.  
  17.  

As it is, I can not use an := overload on this record because the compiler thinks I should use the normal := which will copy the whole record.

 I can implement a method to do this of course but then it is kind of a black sheep of the record because  I have all of the other common overloads already implemented.

The only true wisdom is knowing you know nothing

TRon

  • Hero Member
  • *****
  • Posts: 3650
Re: Impossible to overload assignment of equal type: How to fix that?
« Reply #4 on: July 14, 2024, 05:13:23 am »
Quote
Impossible to overload assignment of equal type: How to fix that?
By modifying the compilers source-code. End user is not allowed to override implicit assignment operator of the same type.
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

Coldzer0

  • Jr. Member
  • **
  • Posts: 53
Re: Impossible to overload assignment of equal type: How to fix that?
« Reply #5 on: July 14, 2024, 05:30:13 am »
You can do it using the operator Copy.

Code: Pascal  [Select][+][-]
  1.   TMyRecord = Record
  2.     X,Y:Integer;
  3.     SomeotherFields:Integer; //That I don't want to copy.
  4.     class operator Copy(constref src: TMyRecord; var dst: TMyRecord);
  5.   End;
  6.  
  7.   class operator TMyRecord.Copy(constref src: TMyRecord; var dst: TMyRecord);
  8.   begin
  9.     dst.X := src.X;
  10.     dst.Y := src.Y;
  11.   end;
  12.  
  13. var
  14.   a, b: TMyRecord;
  15. Begin
  16.   a.X := 1;
  17.   a.Y := 2;
  18.   a.SomeotherFields := 3;
  19.   b.X := 4;
  20.   b.Y := 5;
  21.   b.SomeotherFields := 6;
  22.  
  23.   a := b;
  24.  
  25.   WriteLn(Format('a.X : %d | a.Y : %d | a.SomeotherFields : %d', [a.X, a.Y, a.SomeotherFields])); // will print "a.X : 4 | a.Y : 5 | a.SomeotherFields : 3"
  26. end.
  27.  
« Last Edit: July 14, 2024, 06:20:51 am by Coldzer0 »

TRon

  • Hero Member
  • *****
  • Posts: 3650
Re: Impossible to overload assignment of equal type: How to fix that?
« Reply #6 on: July 14, 2024, 05:48:55 am »
You can do it using operator Copy
Hmz nice find.

afaik only mentioned on the new features pages and the wiki as I was unable to locate it in the documentation.
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

Coldzer0

  • Jr. Member
  • **
  • Posts: 53
Re: Impossible to overload assignment of equal type: How to fix that?
« Reply #7 on: July 14, 2024, 06:00:20 am »
You can do it using operator Copy
Hmz nice find.

afaik only mentioned on the new features pages and the wiki as I was unable to locate it in the documentation.

I found it here https://wiki.freepascal.org/management_operators#Copy

TRon

  • Hero Member
  • *****
  • Posts: 3650
Re: Impossible to overload assignment of equal type: How to fix that?
« Reply #8 on: July 14, 2024, 07:17:25 am »
I found it here https://wiki.freepascal.org/management_operators#Copy
Yeah I found it there as well but unfortunately only after you posted your example because I wasn't aware that management operators existed (and they seem to exist for quite some time) so before that example was posted I had no idea what to look for :)

I can blame that on myself for not looking close enough at the new features page (3.2.0) which links to that page but quite honestly I expected it to be mentioned somewhere in the official documentation (where I was unable to find anything on management operators but perhaps I overlooked).

Than you for bringing the example to my/our attention.
« Last Edit: July 14, 2024, 07:54:42 am by TRon »
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

Thaddy

  • Hero Member
  • *****
  • Posts: 16199
  • Censorship about opinions does not belong here.
Re: Impossible to overload assignment of equal type: How to fix that?
« Reply #9 on: July 14, 2024, 02:39:02 pm »
Unfortunaly the wiki entry is the only thing that comes close to official, although I suspect it is in the trunk/main documentation. But there are already many examples on this forum.
If I smell bad code it usually is bad code and that includes my own code.

jamie

  • Hero Member
  • *****
  • Posts: 6735
Re: Impossible to overload assignment of equal type: How to fix that?
« Reply #10 on: July 14, 2024, 03:31:58 pm »
You can do it using operator Copy
Hmz nice find.

afaik only mentioned on the new features pages and the wiki as I was unable to locate it in the documentation.

I found it here https://wiki.freepascal.org/management_operators#Copy

Yes, that works. Thank you much!

While I was there at that page, I am also interested in the initializers and finalizers, those too, will also come in handy.
 :o
The only true wisdom is knowing you know nothing

PascalDragon

  • Hero Member
  • *****
  • Posts: 5764
  • Compiler Developer
Is there a switch that I don't know about?

No. Operators that are implemented by the compiler are not allowed to be overloaded to avoid potential confusion especially when global and scoped operators are mixed. As others wrote, use the Copy management operator for that as that correctly hooks into the copy mechanism of the RTL.

jamie

  • Hero Member
  • *****
  • Posts: 6735
I was scoping around my Delphi and noticed they call it "Assign" not copy.

Trying to sync source codes with a few compiler processors to keep them happy on both ends.

Thanks.
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 16199
  • Censorship about opinions does not belong here.
Delphi did not have management operators until very recently. Freepascal has it for quite some time. That is the same as that Freepascal had generics way before Delphi.
It is thus quite common for us to see that they did not copy our syntax.
In due time, there probably will be a Delphi syntax compatible mode in {$mode delphi}. That is as usual.
If I smell bad code it usually is bad code and that includes my own code.

Thaddy

  • Hero Member
  • *****
  • Posts: 16199
  • Censorship about opinions does not belong here.
Unless you mean the normal assign for classes.
The advantage of the management operator copy is that it allows the programmer to implement it as a deep copy, a shallow copy, or like in your case, a partial copy, while maintaining the use of the assignment operator to perform that automatically.
« Last Edit: July 15, 2024, 12:18:03 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

 

TinyPortal © 2005-2018