Advanced records and operator overloading
last modified November 18, 2026 Free Pascal 3.2.2
A record can do more than hold fields. With the
advancedrecords mode switch, a record can declare methods,
properties, and private sections, and with the top-level
operator declarations of ObjFPC mode it can define what
+, *, and = mean. The result is a value
type with an interface, close to a small class but copied on assignment and
stored inline. This article builds a vector type in steps.
Record methods
A method is declared inside the record and implemented outside it with the
type name as a prefix. Inside the method, the fields are in scope and
Self refers to the record. The mode switch
{$modeswitch advancedrecords} is required in ObjFPC mode;
without it, the compiler stops at the first method declaration.
program methods;
{$mode objfpc}{$H+}
{$modeswitch advancedrecords}
type
TVector = record
X, Y: Double;
procedure Init(ax, ay: Double);
function Length: Double;
end;
procedure TVector.Init(ax, ay: Double);
begin
X := ax;
Y := ay;
end;
function TVector.Length: Double;
begin
Result := Sqrt(X * X + Y * Y);
end;
var
v: TVector;
begin
v.Init(3, 4);
WriteLn('v = (', v.X:0:1, ', ', v.Y:0:1, ')');
WriteLn('length = ', v.Length:0:1);
end.
Init sets both fields, and Length computes the
Euclidean length. The method call uses the same dot syntax as a field access.
Unlike a class method, a record method does not receive a hidden pointer to a
heap object: the record itself is passed, and Self points to that
copy or to the original depending on how the method is called.
$ fpc -gl methods.pas $ ./methods v = (3.0, 4.0) length = 5.0
Private fields and properties
An advanced record can have private, protected, and
public sections. Fields in the private section are not accessible
outside the record, and properties expose them through read and write
specifiers. The next record keeps its counter private and offers three public
methods.
program visibility;
{$mode objfpc}{$H+}
{$modeswitch advancedrecords}
type
TCounter = record
private
FValue: Integer;
public
procedure Init(start: Integer);
procedure Increment;
function Value: Integer;
end;
procedure TCounter.Init(start: Integer);
begin
FValue := start;
end;
procedure TCounter.Increment;
begin
Inc(FValue);
end;
function TCounter.Value: Integer;
begin
Result := FValue;
end;
var
c: TCounter;
begin
c.Init(10);
c.Increment;
c.Increment;
WriteLn('value = ', c.Value);
end.
The field FValue follows the usual F prefix for a
field. Outside the record, only Init, Increment, and
Value are visible, so the counter cannot be changed except through
Increment. The private section of a record is
enforced by the compiler in the same translation unit; it is not a security
boundary.
$ fpc -gl visibility.pas $ ./visibility value = 12
Top-level operators
ObjFPC mode declares operators at the top level, outside the record. The syntax names the operator, the parameter list, and a result variable after the closing parenthesis. The operator can be used anywhere the expression is visible, and the compiler chooses it from the operand types.
program operators;
{$mode objfpc}{$H+}
type
TVector = record
X, Y: Double;
end;
operator + (const a, b: TVector) c: TVector;
begin
c.X := a.X + b.X;
c.Y := a.Y + b.Y;
end;
operator * (const a: TVector; k: Double) c: TVector;
begin
c.X := a.X * k;
c.Y := a.Y * k;
end;
var
u, v, w: TVector;
begin
u.X := 1; u.Y := 2;
v.X := 3; v.Y := 4;
w := u + v;
WriteLn('u + v = (', w.X:0:1, ', ', w.Y:0:1, ')');
w := u * 2.5;
WriteLn('u * 2.5 = (', w.X:0:1, ', ', w.Y:0:1, ')');
end.
The first operator adds two vectors component by component; the second scales
one vector by a real. The result is a new record, so the operands are not
modified. Both operators receive their arguments as const, which
avoids copying them and documents that they are inputs.
$ fpc -gl operators.pas $ ./operators u + v = (4.0, 6.0) u * 2.5 = (2.5, 5.0)
Comparison and conversion
The equality operator returns a Boolean, and the assignment operator can
convert a record to another type. The next example defines = for
two vectors and := from a vector to a string. The string
conversion is used explicitly with string(u), because
WriteLn does not apply it implicitly here.
program operators2;
{$mode objfpc}{$H+}
uses SysUtils;
type
TVector = record
X, Y: Double;
end;
operator + (const a, b: TVector) c: TVector;
begin
c.X := a.X + b.X;
c.Y := a.Y + b.Y;
end;
operator = (const a, b: TVector) eq: Boolean;
begin
eq := (a.X = b.X) and (a.Y = b.Y);
end;
operator := (const a: TVector) s: string;
begin
s := '(' + FloatToStr(a.X) + ', ' + FloatToStr(a.Y) + ')';
end;
var
u, v: TVector;
begin
u.X := 1; u.Y := 2;
v.X := 1; v.Y := 2;
WriteLn('u = v: ', u = v);
WriteLn('u as string: ', string(u));
end.
u = v compares the components and returns true. The assignment
operator builds a string from the fields, and string(u) invokes it
explicitly. The + operator is repeated here so that the record can
be used in the same way as in the previous section; in a real unit it would be
declared once in the interface.
$ fpc -gl operators2.pas $ ./operators2 u = v: TRUE u as string: (1, 2)
A complete vector type
The final example combines methods, private fields, properties, and operators in one type. The fields are private and are reached through read-only properties, the methods provide initialization and output, and the operators work with the public properties.
program vector;
{$mode objfpc}{$H+}
{$modeswitch advancedrecords}
uses SysUtils;
type
TVector = record
private
FX, FY: Double;
public
procedure Init(ax, ay: Double);
function Length: Double;
function ToString: string;
property X: Double read FX;
property Y: Double read FY;
end;
operator + (const a, b: TVector) c: TVector;
begin
c.Init(a.X + b.X, a.Y + b.Y);
end;
operator * (const a: TVector; k: Double) c: TVector;
begin
c.Init(a.X * k, a.Y * k);
end;
operator = (const a, b: TVector) eq: Boolean;
begin
eq := (a.X = b.X) and (a.Y = b.Y);
end;
procedure TVector.Init(ax, ay: Double);
begin
FX := ax;
FY := ay;
end;
function TVector.Length: Double;
begin
Result := Sqrt(FX * FX + FY * FY);
end;
function TVector.ToString: string;
begin
Result := Format('(%.1f, %.1f)', [FX, FY]);
end;
var
u, v, w: TVector;
begin
u.Init(1, 2);
v.Init(3, 4);
w := u + v;
WriteLn('u + v = ', w.ToString);
w := u * 2.5;
WriteLn('u * 2.5 = ', w.ToString);
WriteLn('u = v: ', u = v);
WriteLn('length of v = ', v.Length:0:1);
end.
The type behaves like a small value object: it is copied by assignment, stored
inline in an array, and needs no destructor. The operators call
Init on their result, which keeps the private fields under the
control of the type. A class would add inheritance and heap allocation; a
record is the lighter choice when neither is needed.
$ fpc -gl vector.pas $ ./vector u + v = (4.0, 6.0) u * 2.5 = (2.5, 5.0) u = v: FALSE length of v = 5.0
Modes and portability
The advancedrecords switch is part of ObjFPC mode and is enabled
per file. Delphi mode enables the same features by default and uses a different
operator syntax: the operators are declared as class operator
inside the record and implemented with the type name as a prefix. Code that
must compile with both modes needs conditional directives or a separate
implementation for each dialect.
Operator overloading is resolved at compile time from the operand types. An
operator that is ambiguous, or a combination that no operator matches, is a
compile-time error rather than a runtime conversion. Keep the set of operators
small and their meaning obvious; a + that also mutates its left
operand surprises readers who expect the arithmetic meaning.
Next steps
The classes article introduces reference types
with constructors, destructors, inheritance, and virtual methods. The
records article covers the record layout and
managed fields that this article builds on, and the
units article shows how to put a type such as
TVector in a reusable unit.
Source
Free Pascal: operator overloading
In this article we have added methods and private fields to records, defined
+, *, =, and := operators,
built a complete vector type, and compared the ObjFPC and Delphi operator
syntax. The series continues with
Pascal objects and classes.
Author
List all Pascal tutorials.