Forum > General

Operator overload in record helper?

(1/1)

Zoë:
Is it possible to overload an operator for a record type after the record is declared in {$MODE DELPHI}?

This code works in Delphi:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Main; {$APPTYPE CONSOLE} uses  SysUtils; type  timespec = record    tv_sec: Integer;    tv_nsec: Integer;  end;   TTimespecHelper = record helper for timespec    class operator LessThan(A, B: timespec): Boolean;  end; class operator TTimespecHelper.LessThan(A, B: timespec): Boolean;begin  Result := (A.tv_sec < B.tv_sec) or ((A.tv_sec = B.tv_sec) and (A.tv_nsec < B.tv_nsec))end; const  A: timespec = (tv_sec: 2; tv_nsec: 123);  B: timespec = (tv_sec: 3; tv_nsec: 123);begin  WriteLn(A < B);end.
If I try to compile it with FPC 3.3.1 using {$MODE DELPHI}, it fails with the errors:


--- Quote ---main.pp(15,11) Error: Procedure or Function expected
main.pp(15,11) Error: An interface, helper or Objective-C protocol or category cannot contain fields
main.pp(15,20) Fatal: Syntax error, ":" expected but "identifier LESSTHAN" found

--- End quote ---

I can get the same effect in {$MODE OBJFPC} by switching from a class helper to a global overload declared like so:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---operator < (A, B: timespec): Boolean;
But that doesn't compile in {$MODE DELPHI} even if I switch from "operator <" to "operator LessThan".

Am I missing something / is there a way to make this work, or is "Support operator overloading in class helpers" just a feature request?  I can make it work by moving the OBJFPC variant into its own unit so I can switch the {$MODE} for just that, or wrapping the record, but I'd like to avoid it if possible.

Thaddy:
No

ASerge:
Create a separate unit where you use OBJFPC. In OBJFPC you can use operator:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode ObjFPC} interface type  TimeSpec = record    tv_sec: Integer;    tv_nsec: Integer;  end; operator < (const A, B: TimeSpec): Boolean; implementation operator < (const A, B: TimeSpec): Boolean;begin  Result := (A.tv_sec < B.tv_sec) or ((A.tv_sec = B.tv_sec) and (A.tv_nsec < B.tv_nsec))end; end. 

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$MODE DELPHI}{$APPTYPE CONSOLE} uses unit1; const  A: TimeSpec = (tv_sec: 2; tv_nsec: 123);  B: TimeSpec = (tv_sec: 3; tv_nsec: 123);begin  WriteLn(A < B);  Readln;end.

Zoë:

--- Quote from: ASerge on May 16, 2024, 04:12:46 am ---Create a separate unit where you use OBJFPC. In OBJFPC you can use operator:

--- End quote ---

Thanks.  I did mention that option in my original post; I just wanted to make sure I wasn't missing something.

My use is simple enough that I opted for the wrapper instead so I wouldn't need to use the second unit everywhere, but I did open a feature request for it in case anyone else needs it:

https://gitlab.com/freepascal.org/fpc/source/-/issues/40788

Navigation

[0] Message Index

Go to full version