Recent

Author Topic: Resolve the conflict  (Read 715 times)

LemonParty

  • Sr. Member
  • ****
  • Posts: 391
Resolve the conflict
« on: September 19, 2025, 07:04:22 pm »
Is this problem can be resolved:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. {$modeswitch advancedrecords}
  3.  
  4. type
  5.   TRec1 = record
  6.     D: DWord;
  7.     class operator :=(constref R: TRec2): TRec1; {Identifier not found "TRec2"}
  8.   end;
  9.  
  10.   TRec2 = record
  11.     W: Word;
  12.     class operator :=(constref R: TRec1): TRec2;
  13.   end;
  14.  
  15.   class operator TRec1.:=(constref R: TRec2): TRec1;
  16.   begin
  17.                
  18.   end;
  19.  
  20.   class operator TRec2.:=(constref R: TRec1): TRec2;
  21.   begin
  22.                
  23.   end;
  24.  
  25. begin
  26.  
  27. end.
  28.  
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

Josh

  • Hero Member
  • *****
  • Posts: 1428
Re: Resolve the conflict
« Reply #1 on: September 19, 2025, 10:08:35 pm »
Code: Pascal  [Select][+][-]
  1.  TRec1 = record
  2.     D: DWord;
  3.     class operator :=(constref R: TRec2): TRec1; {Identifier not found "TRec2"}
  4.   end;

your using trec2 which is not defined yet.

maybe something like
Code: Pascal  [Select][+][-]
  1. type
  2.   TRec1 = record
  3.     D: DWord;
  4.   end;
  5.  
  6.   TRec2 = record
  7.     W: Word;
  8.   end;
  9.  
  10. class operator TRec1.:=(constref R: TRec2): TRec1;
  11. begin
  12.  
  13. end;
  14.  
  15. class operator TRec2.:=(constref R: TRec1): TRec2;
  16. begin
  17.  
  18. end;
  19.  
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

Warfley

  • Hero Member
  • *****
  • Posts: 2031
Re: Resolve the conflict
« Reply #2 on: September 19, 2025, 10:17:24 pm »
Unlike languages like C, Pascal does not support forward type Declarations for non pointer (and class) types

So short answer: Thats simply not possible if you need to use class operators

440bx

  • Hero Member
  • *****
  • Posts: 5894
Re: Resolve the conflict
« Reply #3 on: September 19, 2025, 10:24:07 pm »
That brings the possibility of using pointers to records instead of the record types.

if he used PRec2 instead of TRec2, he could make it work but, it also means the operator applies directly to a pointer type instead of indirectly by using constref.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

LemonParty

  • Sr. Member
  • ****
  • Posts: 391
Re: Resolve the conflict
« Reply #4 on: September 20, 2025, 01:22:33 am »
Thats simply not possible if you need to use class operators
This is limit a feature usage. Very bad.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

runewalsh

  • Full Member
  • ***
  • Posts: 106
Re: Resolve the conflict
« Reply #5 on: September 20, 2025, 03:13:01 am »
Operators can be defined as free functions.

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. type
  3.         TRec1 = record
  4.                 D: DWord;
  5.         end;
  6.  
  7.         TRec2 = record
  8.                 W: Word;
  9.         end;
  10.  
  11.         operator :=(const R: TRec2): TRec1;
  12.         begin
  13.         end;
  14.  
  15.         operator :=(const R: TRec1): TRec2;
  16.         begin
  17.         end;
  18.  
  19. begin
  20. end.

ALLIGATOR

  • Sr. Member
  • ****
  • Posts: 305
  • I use FPC [main] 💪🐯💪
Re: Resolve the conflict
« Reply #6 on: September 20, 2025, 03:23:02 am »
This can be done and it works in Delphi12.1CE, but does not work in FPC [main] :
Code: Pascal  [Select][+][-]
  1. program kek;
  2. {$ifdef FPC}{$mode delphi}{$endif}
  3.  
  4. type
  5.   TRec1 = record
  6.   end;
  7.   TRec2 = record
  8.     class operator Explicit(const R: TRec1): TRec2;
  9.   end;
  10.   TRec1Helper = record helper for TRec1
  11.     class operator Explicit(const R: TRec2): TRec1;
  12.   end;
  13.  
  14. class operator TRec1Helper.Explicit(const R: TRec2): TRec1;
  15. begin
  16. end;
  17.  
  18. class operator TRec2.Explicit(const R: TRec1): TRec2;
  19. begin
  20. end;
  21.  
  22. begin
  23. end.

FPC [main]: FAIL ❌😵
D12.1CE: OK ✅😇

Found an issue on this topic:
https://gitlab.com/freepascal.org/fpc/source/-/issues/40788
I may seem rude - please don't take it personally

AlexK

  • Full Member
  • ***
  • Posts: 101
Re: Resolve the conflict
« Reply #7 on: September 20, 2025, 04:36:02 am »
Unlike languages like C, Pascal does not support forward type Declarations for non pointer (and class) types

So short answer: Thats simply not possible if you need to use class operators

Let it be explicit pointers then.
Code: Pascal  [Select][+][-]
  1. type
  2.   PRec1 = ^TRec1;
  3.   PRec2 = ^TRec2;
  4.  
  5.   TRec1 = record
  6.     D: DWord;
  7.     class operator := (const R: PRec2): TRec1;
  8.   end;
  9.  
  10.   TRec2 = record
  11.     W: word;
  12.     class operator := (const R: PRec1): TRec2;
  13.   end;
  14.  
  15.   class operator TRec1.:=(const R: PRec2): TRec1;
  16.   begin
  17.  
  18.   end;
  19.  
  20.   class operator TRec2.:=(const R: PRec1): TRec2;
  21.   begin
  22.  
  23.   end;
  24.  
  25. begin
  26. end.    
  27.  

2013, https://gitlab.com/freepascal.org/fpc/source/-/issues/24425 :
Quote
Adding extensions that can only be used in very specific scenarios complicate the language (because the programmer has to think about the fact where it can be used and not) and the compiler (it has to add checks for all of these cases), and are generally not a good idea.
« Last Edit: September 20, 2025, 04:45:52 am by AlexK »

Thaddy

  • Hero Member
  • *****
  • Posts: 18524
  • Here stood a man who saw the Elbe and jumped it.
Re: Resolve the conflict
« Reply #8 on: September 20, 2025, 08:18:22 am »
But warn: in the above the records are of different size, so would need quite some extra logic inside the operators.
You can't simply write
Code: Pascal  [Select][+][-]
  1.   class operator TRec1.:=(constref R: PRec2): TRec1;
  2.   begin
  3.      move(R^,Result,SizeOf(Result)); // only possible if size of the records are equal        
  4.   end;
This is only possible if the records are the same size.
« Last Edit: September 20, 2025, 08:28:48 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

 

TinyPortal © 2005-2018