Enumerations and sets in Pascal
last modified November 19, 2026 Free Pascal 3.2.2
An enumeration gives names to a fixed list of values, such as the days of a week or the states of a connection. A set is a collection of distinct values of an ordinal type, and Pascal implements it as a bit mask, so testing membership is a single bit test. The two features work together: an enumeration supplies the values, and a set of that enumeration supplies combinations of them. This article declares enumerations, restricts them with scoped names, builds subrange types, and uses sets of enums and characters.
Declaring an enumeration
An enumeration type lists its values in parentheses, in order. The first value
has ordinal number 0, the next 1, and so on. The values are constants of the
type, so they can be assigned directly, compared with =, and used
as the control variable of a for loop. Ord returns
the position, Succ and Pred move to the neighbour,
and Low and High return the first and last value.
program enums;
{$mode objfpc}{$H+}
type
TColor = (Red, Green, Blue, Yellow);
var
C: TColor;
begin
C := Green;
WriteLn('C = ', C);
WriteLn('Ord(C) = ', Ord(C));
WriteLn('Succ(C) = ', Succ(C));
WriteLn('Pred(C) = ', Pred(C));
WriteLn('Low = ', Low(TColor), ', High = ', High(TColor));
for C := Low(TColor) to High(TColor) do
Write(Ord(C), ':', C, ' ');
WriteLn;
end.
WriteLn prints the name of the enumeration value, not its number,
which makes the output readable without a conversion table. Succ
and Pred do not check the bounds at run time: applying
Succ to the last value produces an ordinal outside the type
instead of raising an error, although a constant expression such as
Succ(Yellow) is rejected at compile time. The loop uses
Low and High rather than the literal names, so it
keeps working if a value is added to the type.
$ fpc -gl enums.pas $ ./enums C = Green Ord(C) = 1 Succ(C) = Blue Pred(C) = Red Low = Red, High = Yellow 0:Red 1:Green 2:Blue 3:Yellow
Scoped enumerations
By default the values of an enumeration are visible in the whole scope where
the type is declared. Two types that both contain a value named Small
would therefore clash. The {$SCOPEDENUMS ON} directive puts the
values inside the type, so they are written TColor.Green and
TSize.Large. The directive is a Free Pascal extension that follows
the Delphi behaviour.
program scoped;
{$mode objfpc}{$H+}
{$SCOPEDENUMS ON}
type
TColor = (Red, Green, Blue);
TSize = (Small, Medium, Large);
var
C: TColor;
S: TSize;
begin
C := TColor.Green;
S := TSize.Large;
WriteLn('C = ', C, ', Ord = ', Ord(C));
WriteLn('S = ', S, ', Ord = ', Ord(S));
if C = TColor.Green then
WriteLn('green selected');
end.
The enumeration values still print by name and still work with
Ord. The only change is that the bare name Green is no
longer accepted; the type prefix is required. This is useful in larger units,
where several enumerations may share common words such as None,
Unknown, or Small.
$ fpc -gl scoped.pas $ ./scoped C = Green, Ord = 1 S = Large, Ord = 2 green selected
Names and values at run time
The names of an enumeration are compile-time information, but the RTL stores
them in the type information that the compiler emits. The TypInfo
unit reads that information: GetEnumName converts an ordinal value
to its name, and GetEnumValue converts a name back to its ordinal
value. The same unit is used by property editors and by code that reads
configuration files.
program enumtext;
{$mode objfpc}{$H+}
uses TypInfo, SysUtils;
type
TColor = (Red, Green, Blue, Yellow);
var
C: TColor;
N: Integer;
begin
C := Blue;
WriteLn('name: ', GetEnumName(TypeInfo(TColor), Ord(C)));
N := GetEnumValue(TypeInfo(TColor), 'Yellow');
WriteLn('Yellow has value ', N);
C := TColor(0);
WriteLn('value 0 is ', C);
WriteLn('TypeInfo size: ', SizeOf(TColor));
end.
GetEnumValue returns -1 when the name is not part of the type, so
a caller must test the result before casting it back. The cast
TColor(0) is unchecked: with range checking enabled, an ordinal
outside the type would raise a runtime error, but by default it produces an
invalid value. SizeOf(TColor) is 4 even though four values would
fit in a byte, because Free Pascal uses a 32-bit integer as the default
representation for an enumeration.
$ fpc -gl enumtext.pas $ ./enumtext name: Blue Yellow has value 3 value 0 is Red TypeInfo size: 4
Subrange types
A subrange type restricts an ordinal type to a contiguous interval, written
Low..High. TDigit = 0..9 is an integer that should
hold only one decimal digit, and TPercent = 0..100 is an integer
in the percentage range. The compiler uses the smallest storage that fits, so a
digit occupies one byte. Assignments outside the range are only caught when
range checking is enabled with {$R+} or -Cr.
program subrange;
{$mode objfpc}{$H+}
type
TDigit = 0..9;
TPercent = 0..100;
function ClampPercent(Value: Integer): TPercent;
begin
if Value < 0 then
Result := Low(TPercent)
else if Value > 100 then
Result := High(TPercent)
else
Result := Value;
end;
var
P: TPercent;
D: TDigit;
begin
P := ClampPercent(120);
WriteLn('P = ', P);
D := 7;
WriteLn('D = ', D, ', SizeOf(TDigit) = ', SizeOf(TDigit));
WriteLn('Low = ', Low(TPercent), ', High = ', High(TPercent));
end.
ClampPercent returns a value that is guaranteed to fit the
subrange, so the assignment to P is safe even with checking
enabled. Subranges document an invariant and reduce storage, but they do not
validate input on their own: the conversion from Integer in the
last branch is where the programmer asserts that the value is in range.
$ fpc -gl subrange.pas $ ./subrange P = 100 D = 7, SizeOf(TDigit) = 1 Low = 0, High = 100
Sets of enumeration values
A set type is declared set of TColor, where TColor is
an ordinal type with at most 256 values. A set value is written as a list in
square brackets, and the empty set is []. The operators combine
sets: + is union, * is intersection, -
is difference, and <= tests whether one set is a subset of
another. The in operator tests one value against a set.
program sets;
{$mode objfpc}{$H+}
type
TColor = (Red, Green, Blue, Yellow);
TColors = set of TColor;
procedure Show(const Title: string; S: TColors);
var
C: TColor;
begin
Write(Title, ': ');
for C := Low(TColor) to High(TColor) do
if C in S then
Write(C, ' ');
WriteLn;
end;
var
Warm, Cool, Both, Only: TColors;
begin
Warm := [Red, Yellow];
Cool := [Green, Blue];
Show('warm', Warm);
Show('cool', Cool);
Both := Warm + Cool;
Show('union', Both);
Only := Warm - Cool;
Show('difference', Only);
if [Red, Yellow] <= Warm then
WriteLn('Red and Yellow are both warm');
Include(Cool, Yellow);
Show('cool after Include', Cool);
Exclude(Cool, Green);
Show('cool after Exclude', Cool);
end.
The set is stored as a bit mask with one bit per possible value, so membership
and set operations are constant-time bit operations regardless of how many
values are in the set. Include and Exclude modify a
set variable in place and are equivalent to S := S + [C] and
S := S - [C], but they read better and avoid a temporary. The
Show procedure iterates over all values and prints only the
members, which is the standard way to display a set.
$ fpc -gl sets.pas $ ./sets warm: Red Yellow cool: Green Blue union: Red Green Blue Yellow difference: Red Yellow Red and Yellow are both warm cool after Include: Green Blue Yellow cool after Exclude: Blue Yellow
Sets of characters
set of Char is the largest useful character set, with one bit for
every byte value. It is the natural tool for counting distinct characters,
classifying input, and checking whether a character belongs to a group. The
range syntax ['a'..'z'] builds a set of all lowercase letters in
one expression.
program charset;
{$mode objfpc}{$H+}
type
TCharSet = set of Char;
function UniqueChars(const S: string): Integer;
var
Seen: TCharSet;
C: Char;
begin
Seen := [];
for C in S do
Include(Seen, C);
Result := 0;
for C := Low(Char) to High(Char) do
if C in Seen then
Inc(Result);
end;
var
Text: string;
begin
Text := 'mississippi';
WriteLn('text: ', Text);
WriteLn('unique characters: ', UniqueChars(Text));
if ['a'..'z'] * [Text[1]] <> [] then
WriteLn('first character is lower case');
end.
The first loop inserts every character into the set, which removes duplicates
by construction; the second loop counts the members. The final test intersects
the lowercase range with a set containing the first character and compares the
result with the empty set. With a 256-bit set of Char, each of
these operations touches four 64-bit words at most.
$ fpc -gl charset.pas $ ./charset text: mississippi unique characters: 4 first character is lower case
Next steps
Enumerations and sets are used by the case statement, which can branch on enum values, and by the bit manipulation article, which explains the masks behind the set representation. The records article shows how enum and set fields are stored inside a record, and the random numbers article uses sets to track which values have already been drawn.
Source
Free Pascal Reference guide: enumeration types
Free Pascal Reference guide: set types
In this article we declared enumerations, used scoped enum members, converted
between names and ordinals, restricted integers with subranges, combined enum
values with sets, and counted characters with a character set. Dynamic bit
collections with TBits are covered in the
bit manipulation article. The series continues with
date and time in Pascal.
Author
List all Pascal tutorials.