Page 1 of 3 123 LastLast
Results 1 to 10 of 24

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

  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

  7. #7
    Quote Originally Posted by JSoftware View Post
    Yes, no problem. If you're going to use a newer compiler, I would advice you to upgrade lazarus to trunk version too anyway
    LOL this sounds like more work than I wanted to do...I will look into it

    So if I upgraded Lazarus to the trunk version, would this have freepascal 2.5.1 with it? Or would I have to download both separately from SVN?

    cheers,
    Paul

  8. #8
    PGD Staff code_glitch's Avatar
    Join Date
    Oct 2009
    Location
    UK (England, the bigger bit)
    Posts
    933
    Blog Entries
    45
    Hang on j, you mean we're already at 2.5.x WTF? I'm still getting used to everything in 2.4.2....
    I once tried to change the world. But they wouldn't give me the source code. Damned evil cunning.

  9. #9
    It's been 2.5.1 for a very long time. That's just the version number for the trunk version

    Lazarus and FPC are seperate products. Release versions of FPC are just bundled with the Lazarus releases so it's possible to compile programs
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  10. #10
    If you're on Windows (32-bit version), here's .bat file I use to compile FPC trunk after I update SVN (just adjust FPCMAKEPATH, FPCPATH and OUTPATH - in this case, C:/dev/fpc/2.4.2 is location of the latest official FPC, and C:/dev/fpc/2.5.1 is location where I want FPC to be installed for later use). It does full clean-make-install cycle.

    set FPCMAKEPATH=C:/dev/fpc/2.4.2
    set FPCPATH=C:/dev/fpc/2.4.2
    set OUTPATH=C:/dev/fpc/2.5.1
    for /r %OUTPATH%\examples %%d in (.svn) do @rmdir /s /q %%d
    %FPCMAKEPATH%/bin/i386-win32/make clean FPC=%FPCPATH%/bin/i386-win32/ppc386.exe
    if errorlevel 1 goto quit
    %FPCMAKEPATH%/bin/i386-win32/make all FPC=%FPCPATH%/bin/i386-win32/ppc386.exe
    if errorlevel 1 goto quit
    %FPCMAKEPATH%/bin/i386-win32/make install FPC=%FPCPATH%/bin/i386-win32/ppc386.exe INSTALL_BASEDIR=%OUTPATH%
    for /r %OUTPATH%\examples %%d in (.svn) do @rmdir /s /q %%d
    :quit

    As of Lazarus, just point FPC directory to C:/dev/fpc/2.5.1 (or wherever you set OUTPATH in the above .bat file), and use built-in feature to recompile it.
    blog: http://alexionne.blogspot.com/

Page 1 of 3 123 LastLast

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
  •