Ada generics
last modified November 23, 2026
A generic unit is a template with parameters that stand for types,
subprograms, and values. The compiler checks the template once, and each
instantiation checks the actual parameters and produces a separate unit with
its own name and types. This gives reuse without the loose typing of a
container that stores addresses: a stack of integers and a stack of words are
distinct types, and the compiler still knows what they contain. This article
covers generic procedures and packages, formal types, formal subprograms, and
instantiation with is new.
A generic procedure
A generic declaration names the formal parameters and the subprogram that
follows them. The declaration and its body may sit directly in the declarative
part of a procedure, which keeps the first example in one file. The formal
type T is private promises only that values of T can
be assigned and compared for equality; the body relies on nothing else.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Swap_Demo is
generic
type T is private;
procedure Swap (A, B : in out T);
procedure Swap (A, B : in out T) is
Temp : constant T := A;
begin
A := B;
B := Temp;
end Swap;
procedure Swap_Int is new Swap (Integer);
A : Integer := 3;
B : Integer := 8;
begin
Swap_Int (A, B);
Put_Line (Integer'Image (A) & " " & Integer'Image (B));
end Swap_Demo;
The instantiation procedure Swap_Int is new Swap (Integer);
creates the procedure that the program calls. Swap itself is not a
callable subprogram; it is a template. The call swaps the two integers, and the
output shows the new values with the leading blank that
Integer'Image puts before a non-negative number.
$ gnatmake -gnat2022 -gnatwa swap_demo.adb $ ./swap_demo 8 3
One generic, several types
The same template can be instantiated more than once, with a different actual
type each time. The instances do not share objects or names, but they share the
generic body, which is compiled once. A formal type declared
limited private would allow neither assignment nor equality, so
private is the weaker promise that this body needs.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Swap_Many is
generic
type T is private;
procedure Swap (A, B : in out T);
procedure Swap (A, B : in out T) is
Temp : constant T := A;
begin
A := B;
B := Temp;
end Swap;
procedure Swap_Int is new Swap (Integer);
procedure Swap_Flt is new Swap (Float);
I1 : Integer := 3;
I2 : Integer := 8;
F1 : Float := 1.5;
F2 : Float := 4.25;
begin
Swap_Int (I1, I2);
Swap_Flt (F1, F2);
Put_Line (Integer'Image (I1) & " " & Integer'Image (I2));
Put_Line (Float'Image (F1) & " " & Float'Image (F2));
end Swap_Many;
The two instantiations produce different subprograms, and the compiler rejects
a call that mixes them: Swap_Int cannot take a
Float. The real output uses the default six-significant-digit
form of Float'Image, which is why the values appear as
4.25000E+00 and 1.50000E+00.
$ gnatmake -gnat2022 -gnatwa swap_many.adb $ ./swap_many 8 3 4.25000E+00 1.50000E+00
A generic library unit
A generic that other units use belongs in a library package or, as here, in a
library procedure. The specification goes into swap.ads and the
body into swap.adb; the unit name Swap determines
both file names. A client writes with Swap; and instantiates the
generic in its own declarative part.
generic type T is private; procedure Swap (A, B : in out T);
The specification holds the generic formal part and the profile of the
procedure. It contains no code; it only states that Swap works for
any type T that supports assignment.
procedure Swap (A, B : in out T) is Temp : constant T := A; begin A := B; B := Temp; end Swap;
The body repeats the profile and swaps the two values through a constant.
It refers to T freely, because the formal type is visible from the
specification.
with Ada.Text_IO; with Swap; use Ada.Text_IO; procedure Swap_Main is procedure Swap_Int is new Swap (Integer); A : Integer := 3; B : Integer := 8; begin Swap_Int (A, B); Put_Line (Integer'Image (A) & " " & Integer'Image (B)); end Swap_Main;
The body has no with clause and no name before
procedure Swap, because the specification already supplies the
formal part. gnatmake swap_main.adb compiles the client, then the
generic body, because the binder has to know which body belongs to the
instantiation. The executable behaves like the first example, now with the
generic in a reusable unit.
$ gnatmake -gnat2022 -gnatwa swap_main.adb $ ./swap_main 8 3
A formal subprogram
A generic can also take a subprogram as a parameter. The declaration below
asks for a function "<" and gives it the default
is <>, which means that the operator visible at the point of
instantiation is used when no actual is supplied. The body calls
A < B without knowing which function will implement it.
generic type T is private; with function "<" (Left, Right : T) return Boolean is <>; function Larger (A, B : T) return T;
The specification adds a formal function to the formal part. Any
instance must provide a "<" for its type, either explicitly or
through the default.
function Larger (A, B : T) return T is
begin
if A < B then
return B;
else
return A;
end if;
end Larger;
The body compares the two arguments with the formal operator and returns the greater one. Which comparison runs is decided at instantiation, not here.
with Ada.Text_IO;
with Larger;
use Ada.Text_IO;
procedure Larger_Main is
type Version is record
Major : Natural := 1;
Minor : Natural := 0;
end record;
function Before (A, B : Version) return Boolean is
begin
return A.Major < B.Major
or else (A.Major = B.Major and then A.Minor < B.Minor);
end Before;
function Trimmed (N : Natural) return String is
S : constant String := Natural'Image (N);
begin
return S (S'First + 1 .. S'Last);
end Trimmed;
function Larger_Int is new Larger (Integer);
function Larger_Ver is new Larger (Version, Before);
V1 : constant Version := (Major => 1, Minor => 4);
V2 : constant Version := (Major => 2, Minor => 0);
Big : constant Version := Larger_Ver (V1, V2);
begin
Put_Line ("larger int =" & Integer'Image (Larger_Int (3, 9)));
Put_Line ("larger version = " & Trimmed (Big.Major) & "."
& Trimmed (Big.Minor));
end Larger_Main;
Larger_Int uses the predefined "<" for integers,
so the default formal is selected. Larger_Ver supplies
Before, which compares the major numbers first and the minor
numbers only when the majors are equal. The record aggregate for
Version uses named components, and Trimmed strips the
blank that Natural'Image puts before each non-negative value.
$ gnatmake -gnat2022 -gnatwa larger_main.adb $ ./larger_main larger int = 9 larger version = 2.0
A generic package
A generic package can take both a type and a value. The package below is a
bounded stack: Element is the type of the items,
Capacity is the maximum number, and the private part holds the
array and the index of the top. Clients see the operations and the private
type Stack, but not the representation.
generic
type Element is private;
Capacity : Positive;
package Stacks is
type Stack is private;
procedure Push (S : in out Stack; E : Element);
function Pop (S : in out Stack) return Element;
function Is_Empty (S : Stack) return Boolean;
function Count (S : Stack) return Natural;
private
type Content is array (1 .. Capacity) of Element;
type Stack is record
Data : Content;
Top : Natural := 0;
end record;
end Stacks;
The specification exports the type Stack and four
operations. The formal object Capacity fixes the bounds of the
array in the private part.
package body Stacks is
procedure Push (S : in out Stack; E : Element) is
begin
if S.Top = Capacity then
raise Constraint_Error with "stack is full";
end if;
S.Top := S.Top + 1;
S.Data (S.Top) := E;
end Push;
function Pop (S : in out Stack) return Element is
E : Element;
begin
if S.Top = 0 then
raise Constraint_Error with "stack is empty";
end if;
E := S.Data (S.Top);
S.Top := S.Top - 1;
return E;
end Pop;
function Is_Empty (S : Stack) return Boolean is
begin
return S.Top = 0;
end Is_Empty;
function Count (S : Stack) return Natural is
begin
return S.Top;
end Count;
end Stacks;
The body implements the operations on the private record. Push
and Pop move Top up and down and refuse to go past
either end of the array.
with Ada.Text_IO;
with Stacks;
use Ada.Text_IO;
procedure Stack_Main is
package Int_Stacks is new Stacks (Integer, 5);
S : Int_Stacks.Stack;
begin
for I in 1 .. 3 loop
Int_Stacks.Push (S, I * 10);
end loop;
Put_Line ("count =" & Natural'Image (Int_Stacks.Count (S)));
Put_Line ("top =" & Integer'Image (Int_Stacks.Pop (S)));
Put_Line ("next =" & Integer'Image (Int_Stacks.Pop (S)));
Put_Line ("empty =" & Boolean'Image (Int_Stacks.Is_Empty (S)));
end Stack_Main;
The instantiation supplies Integer for Element and
5 for Capacity, so the private array has five
components. Push and Pop check the limits before they
touch the array, and both raise Constraint_Error with a message
when the operation is not possible. The object S starts empty
because Top has the default zero.
$ gnatmake -gnat2022 -gnatwa stack_main.adb $ ./stack_main count = 3 top = 30 next = 20 empty =FALSE
A second instance and a limit
The element type of the stack must be definite, because the private part
declares an array of Element values. Instantiating with
String therefore fails at compile time with "unconstrained element
type in array declaration" and "actual for Element must be a definite
subtype". A fixed subtype, such as Word below, has a known length
and is accepted.
with Ada.Text_IO;
with Ada.Exceptions;
with Stacks;
use Ada.Text_IO;
procedure Stack_Words is
subtype Word is String (1 .. 5);
package Word_Stacks is new Stacks (Word, 2);
S : Word_Stacks.Stack;
begin
Word_Stacks.Push (S, "alpha");
Word_Stacks.Push (S, "bravo");
Put_Line ("count =" & Natural'Image (Word_Stacks.Count (S)));
Put_Line ("top =" & Word_Stacks.Pop (S));
Word_Stacks.Push (S, "again");
Word_Stacks.Push (S, "fifth");
exception
when E : Constraint_Error =>
Put_Line ("error:" & Ada.Exceptions.Exception_Message (E));
end Stack_Words;
After one pop the stack has room for one more word, so the push of
"again" succeeds and the push of "fifth" reaches the
capacity and raises. The handler catches the exception and prints the message
that Push supplied, stack is full. The integer stack
and the word stack are separate instances of the same generic, with distinct
types and no shared objects.
$ gnatmake -gnat2022 -gnatwa stack_words.adb $ ./stack_words count = 2 top =bravo error:stack is full
Next steps
The next article uses the standard containers, which are themselves generic packages. It covers vectors, doubly linked lists, ordered sets, ordered maps, and hashed maps, together with the cursors that select an element in a container.
Source
In this article we have written a generic swap procedure, instantiated it for several types, moved it into a library unit, supplied a formal comparison function, built a generic bounded stack, and used a second instance with a fixed-length string subtype. The packages article explains the specification and body files that the library examples use.
Author
List all Ada tutorials.