Skip to content

Allow operator overloading via helpers in Delphi mode

Summary

Delphi mode should support operator overloading via type helpers. Delphi itself supports it. In ObjFPC, you can use a global overload instead, but those aren't supported in Delphi mode. As it is, if there's an existing record you need to extend, you either need to wrap it or add a second ObjFPC mode unit that provides the global overloads and include it everywhere.

Example Project

    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.
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information