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
Bookmarks