Is it possible to overload an operator for a record type after the record is declared in {$MODE DELPHI}?
This code works in Delphi:
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:
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
I can get the same effect in {$MODE OBJFPC} by switching from a class helper to a global overload declared like so:
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.