Results 1 to 10 of 24

Thread: Class operators in Freepascal records like in Delphi 2010?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Class operators in Freepascal records like in Delphi 2010?

    Hey all,
    I was wondering if it is possible to use records in Freepascal (Lazarus 0.9.30, Win32) with class operators now like Delphi 2010?

    Code:
    interface
    
    type
      TVector2f = record
      public
        x, y: Single;
        class operator Negative(const v: TVector2f): TVector2f;
        class operator Add(const v1, v2: TVector2f): TVector2f;
        class operator Subtract(const v1, v2: TVector2f): TVector2f;
        class operator Multiply(const v: TVector2f; const s: Single): TVector2f;
        procedure SetValue(const aX, aY: Single);
        function Normalize: TVector2f;
        function Len: Single;
        function Dot(const v: TVector2f): Single;
        function Perp: TVector2f;
      end;
    Code:
    implementation
    
    //
    // TVector2f routines
    //
    function Vector2f(aX, aY: Single): TVector2f;
    begin
      Result.x := aX;
      Result.y := aY;
    end;
    
    class operator TVector2f.Negative(const v: TVector2f): TVector2f;
    begin
      Result.x := -v.x;
      Result.y := -v.y;
    end;
    
    class operator TVector2f.Add(const v1, v2: TVector2f): TVector2f;
    begin
      Result.x := v1.x + v2.x;
      Result.y := v1.y + v2.y;
    end;
    
    class operator TVector2f.Subtract(const v1, v2: TVector2f): TVector2f;
    begin
      Result.x := v1.x - v2.x;
      Result.y := v1.y - v2.y;
    end;
    
    class operator TVector2f.Multiply(const v: TVector2f; const s: Single): TVector2f;
    begin
      Result.x := v.x * s;
      Result.y := v.y * s;
    end;
    
    procedure TVector2f.SetValue(const aX, aY: Single);
    begin
      x := aX;
      y := aY;
    end;
    
    function TVector2f.Normalize: TVector2f;
    var
      mag: Single;
    begin
      mag := Len;
      if mag < 1 then mag := 1;
      x := x / mag;
      y := y / mag;
    
      Result.x := x;
      Result.y := y;
    end;
    
    function TVector2f.Len: Single;
    begin
      Result := Sqrt(Sqr(x) + Sqr(y));
    end;
    
    function TVector2f.Dot(const v: TVector2f): Single;
    begin
      Result := x * v.x + y * v.y;
    end;
    
    function TVector2f.Perp: TVector2f;
    begin
      Result.SetValue(-y, x);
    end;
    I have tried, but it doesn't seem possible - the compiler is complaining

    It says it does here, but I don't know if it is the version that comes with Lazarus 0.9.30:
    http://wiki.freepascal.org/FPC_New_F..._record_syntax

    cheers,
    Paul

  2. #2
    It works fine with FPC 2.5.1(trunk version)
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  3. #3
    Quote Originally Posted by JSoftware View Post
    It works fine with FPC 2.5.1(trunk version)
    Bummer...Lazarus 0.9.30 seems to be using Freepascal 2.4.2



    cheers,
    Paul

  4. #4
    Just rebuild FPC from source

    It's easy
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  5. #5
    Quote Originally Posted by JSoftware View Post
    Just rebuild FPC from source

    It's easy
    Can I do that AND use the new compiler in Lazarus; overriding the one that comes with it?

    cheers,
    Paul

  6. #6
    Yes, no problem. If you're going to use a newer compiler, I would advice you to upgrade lazarus to trunk version too anyway
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •