ZetCode

What is new in Free Pascal 3.2.2

last modified November 19, 2026 Free Pascal 3.2.2

Free Pascal 3.2.2 was released on May 20, 2021. It is a point release, so most of its language features arrived earlier in the 3.2 series, and the 3.2.2 changes themselves are few: a new macOS/AArch64 target, record methods assigned to method variables, thread names for the debugger, and MySQL 8.0 support in SQLdb. This article separates the two, shows each feature with a program that runs, and lists what the compiler still does not accept.

What 3.2.2 itself adds

The 3.2.2 release notes list four items. The compiler can target macOS on AArch64 (Apple Silicon), TThread.NameThreadForDebugging gives a thread a name that appears in the debugger, a record method can be assigned to a method variable, and SQLdb can talk to MySQL 8.0. The other features described below belong to the 3.2 series or predate it, and all are available in 3.2.2.

The first language change is small but useful: a method of a record can be stored in a method pointer. In ObjFPC the address operator is still required, because the bare method name is parsed as a call.

recmethod.pas
program recmethod;
{$mode objfpc}{$H+}
{$modeswitch advancedrecords}

type
  TCounter = record
    Value: Integer;
    function Next: Integer;
  end;
  TNextFunc = function: Integer of object;

function TCounter.Next: Integer;
begin
  Inc(Value);
  Result := Value;
end;

var
  C: TCounter;
  F: TNextFunc;

begin
  C.Value := 40;
  F := @C.Next;
  WriteLn(F());
  WriteLn(F());
end.

The method pointer keeps the address of the record as its data pointer, so the two calls increment the same C and return 41 and 42. A method pointer to a local record must not outlive the record; here both live until the program ends.

$ fpc -gl recmethod.pas
$ ./recmethod
41
42

Thread names in the debugger

Thread names are set from inside Execute and are visible in gdb and other debuggers. The call has no effect on the program's output, so the example only proves that it compiles and runs on Linux.

namedthread.pas
program namedthread;
{$mode objfpc}{$H+}

uses
  cthreads, Classes, SysUtils;

type
  TWorker = class(TThread)
  protected
    procedure Execute; override;
  end;

procedure TWorker.Execute;
begin
  NameThreadForDebugging('worker-1');
  WriteLn('worker running');
end;

var
  W: TWorker;

begin
  W := TWorker.Create(True);
  try
    W.Start;
    W.WaitFor;
  finally
    W.Free;
  end;
end.

NameThreadForDebugging is implemented on Windows, Linux, and Android; other platforms accept the call but may ignore it. The cthreads unit must come first in uses, as in every threaded program on Unix.

$ fpc -gl namedthread.pas
$ ./namedthread
worker running

Dynamic array operations

The 3.2.0 compiler added Insert, Delete, and Concat for dynamic arrays, array constants and constructors, and the + operator behind the arrayoperators mode switch. The program below builds an array with the bracket constructor, inserts an element, removes one, and concatenates two arrays.

dynops.pas
program dynops;
{$mode objfpc}{$H+}
{$modeswitch arrayoperators}

var
  A, B, C: array of Integer;
  I: Integer;

begin
  A := [1, 2, 3];
  B := [4, 5];
  Insert(9, A, 1);
  Delete(B, 0, 1);
  C := A + B;
  for I := 0 to High(C) do
    Write(C[I], ' ');
  WriteLn;
end.

Insert(9, A, 1) puts the value at index 1, so the array becomes 1, 9, 2, 3. Delete(B, 0, 1) removes the first element, leaving 5. The concatenation produces 1, 9, 2, 3, 5. The indexes are zero-based, unlike string indexes in Pascal.

$ fpc -gl dynops.pas
$ ./dynops
1 9 2 3 5

Generic routines

Before 3.2, generics were limited to types and their methods. A standalone generic function is now declared with the generic keyword and called through specialize, so one body serves every type that supports the operations it uses.

genfunc.pas
program genfunc;
{$mode objfpc}{$H+}

generic function MaxOf<T>(A, B: T): T;
begin
  if A > B then
    Result := A
  else
    Result := B;
end;

begin
  WriteLn(specialize MaxOf<Integer>(19, 23));
  WriteLn(specialize MaxOf<string>('alpha', 'beta'));
end.

The compiler instantiates the body once for Integer and once for string. The > operator must exist for the type; there are no constraints on the generic parameter in this syntax, so a type without comparison fails at specialization time. Delphi mode uses its own syntax without generic and specialize.

$ fpc -gl genfunc.pas
$ ./genfunc
23
beta

Scoped enumerations

{$SCOPEDENUMS ON} puts enumeration values in the scope of their type, so Green must be written as TColor.Green. This prevents two enums with a common value name from colliding, and it makes the type of a constant obvious at the call site.

scoped2.pas
program scoped2;
{$mode objfpc}{$H+}
{$SCOPEDENUMS ON}

type
  TColor = (Red, Green, Blue);

var
  C: TColor;

begin
  C := TColor.Green;
  WriteLn(C);
  WriteLn(Succ(C));
end.

Succ follows the declaration order, so the successor of Green is Blue. Ord, Pred, Low, and High work as before, and WriteLn prints the value name without the type prefix.

$ fpc -gl scoped2.pas
$ ./scoped2
Green
Blue

Type helpers

A type helper adds methods to an existing type without changing its declaration. ObjFPC requires {$modeswitch typehelpers}; Delphi mode enables the syntax by default. The helper below adds Doubled to every Integer.

helper.pas
program helper;
{$mode objfpc}{$H+}
{$modeswitch typehelpers}

type
  TIntHelper = type helper for Integer
    function Doubled: Integer;
  end;

function TIntHelper.Doubled: Integer;
begin
  Result := Self * 2;
end;

var
  N: Integer;

begin
  N := 21;
  WriteLn(N.Doubled);
end.

Self is the integer the method was called on. A helper cannot add fields, so it is a way to add behaviour rather than state. Two helpers for the same type in one scope conflict, and a helper for a class does not change the class's layout.

$ fpc -gl helper.pas
$ ./helper
42

Unicode strings

UnicodeString is a distinct string type in the 3.2 series. On Linux and other Unix targets it is UTF-8 encoded, so Length counts bytes and indexing returns bytes; on Windows the same type is UTF-16. The program below converts between a UnicodeString and a plain UTF-8 string.

unicodestr.pas
program unicodestr;
{$mode objfpc}{$H+}

uses
  SysUtils;

var
  U: UnicodeString;
  S: string;

begin
  U := 'Příliš žluťoučký kůň';
  S := string(U);
  WriteLn('UnicodeString: ', Length(U));
  WriteLn('UTF-8 string:  ', Length(S));
  WriteLn(S);
end.

The text has 20 characters but 29 bytes in UTF-8, and both types report 29 on this platform because both store the same UTF-8 bytes. The assignment S := string(U) converts according to the code page of the AnsiString; on Windows the source is UTF-16 and the result uses the system code page, which is not necessarily UTF-8.

$ fpc -gl unicodestr.pas
$ ./unicodestr
UnicodeString: 29
UTF-8 string:  29
Příliš žluťoučký kůň

What is not in 3.2.2

Anonymous methods and function references are Delphi features that Free Pascal 3.2.2 does not implement. A reference to function type is rejected even in Delphi mode with the anonymous-function mode switch enabled. Code that depends on closures must keep using method pointers or objects with methods.

anon2.pas
program anon2;
{$mode delphi}

type
  TFunc = reference to function(X: Integer): Integer;

begin
end.

The compiler does not recognize reference as a type constructor, so it fails at the first to. Class operators inside records, direct procedural assignment, and the other Delphi syntax described in the dialects article do work; the missing piece is specifically the anonymous function.

$ fpc anon2.pas
anon2.pas(4,11) Error: Identifier not found "reference"
anon2.pas(4,21) Error: Error in type definition
anon2.pas(4,21) Fatal: Syntax error, ";" expected but "TO" found
Fatal: Compilation aborted

Upgrading

A project that already compiles with 3.2.0 should build with 3.2.2 without source changes. Rebuild the units, because .ppu files are tied to the compiler version, and check the release notes for the changed behaviour of SeekEof, TBufferedFileStream.Seek, and the Boyer-Moore matching in StrUtils. The new dynamic array and generic features are additive, so they can be adopted one unit at a time.

Source

Free Pascal: new features in 3.2.2

Free Pascal: new features in 3.2.0

Free Pascal Reference guide

In this article we separated the 3.2.2 changes from the 3.2.0 features, assigned a record method to a method pointer, named a thread, used dynamic array operations and generic routines, and confirmed that anonymous functions are still absent. The dialects article covers the syntax differences between the modes, and the generics article goes deeper into generic types.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Pascal tutorials.