Ada packages
last modified November 23, 2026
A package collects declarations under one name. It has a specification, which
is the part other units may see, and a body, which supplies the
implementations. The specification can also hide details in a private part, so
a type is usable without exposing how it is represented. Packages are the main
unit of reuse in Ada: most of the standard library, including
Ada.Text_IO, is organised as packages. This article starts with a
package nested inside a program and then moves it into its own files.
A package inside a program
A package may be declared in the declarative part of a procedure, like any other declaration. The specification lists the constant and the function that clients may use; the body supplies the function implementation. Both parts end with the package name, which makes long declarations easier to check.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Local_Pack is
package Geometry is
Pi : constant Float := 3.14159;
function Circle_Area (Radius : Float) return Float;
end Geometry;
package body Geometry is
function Circle_Area (Radius : Float) return Float is
(Pi * Radius * Radius);
end Geometry;
begin
Put_Line ("area =" & Float'Image (Geometry.Circle_Area (2.0)));
end Local_Pack;
The function is declared in the specification and defined in the body, and the
caller qualifies it with the package name, Geometry.Circle_Area.
The constant Pi is visible inside the body without qualification
because the body belongs to the same package. The area printed is that of a
circle with radius 2.
$ gnatmake -gnat2022 -gnatwa local_pack.adb $ ./local_pack area = 1.25664E+01
A library package
A package meant for reuse lives outside the main program. Its specification
goes into a file named after the package with the extension .ads,
and its body into the matching .adb file. A client unit names the
package in a with clause, which makes the package visible but does
not put its declarations directly into scope.
package Shapes is Pi : constant Float := 3.14159; function Circle_Area (Radius : Float) return Float; function Ring_Area (Outer, Inner : Float) return Float; end Shapes;
The specification is the contract: it declares a constant and two functions without saying how they are computed. A client can compile against this file alone, and the body can be replaced as long as the declarations keep their names, types, and parameter profiles.
package body Shapes is
function Circle_Area (Radius : Float) return Float is
(Pi * Radius * Radius);
function Ring_Area (Outer, Inner : Float) return Float is
(Circle_Area (Outer) - Circle_Area (Inner));
end Shapes;
The body repeats each declaration's profile and adds the implementation, and it
may call one function from another, as
Ring_Area calls Circle_Area. It can also contain
private helpers that the specification never mentions.
with Ada.Text_IO;
with Shapes;
use Ada.Text_IO;
procedure Area_Main is
begin
Put_Line ("circle =" & Float'Image (Shapes.Circle_Area (2.0)));
Put_Line ("ring =" & Float'Image (Shapes.Ring_Area (3.0, 2.0)));
end Area_Main;
The main program names both packages in with clauses. Since there
is no use Shapes, each call carries the package name. Building
area_main.adb is enough: gnatmake follows the
with clauses, finds shapes.ads and
shapes.adb, and compiles them in the right order. The ring area is
the difference between the areas of circles with radii 3 and 2.
$ gnatmake -gnat2022 -gnatwa area_main.adb $ ./area_main circle = 1.25664E+01 ring = 1.57080E+01
The private part
A specification may end with a private section. Declarations before
it are visible to clients; declarations after it are visible only to the package
body. This is how a package exports a type without
revealing its representation: the type is declared as private in
the visible part, and its full definition appears in the private part.
package Counters is type Counter is private; Zero : constant Counter; procedure Increment (C : in out Counter); function Value (C : Counter) return Integer; private type Counter is range 0 .. 1_000_000; Zero : constant Counter := 0; end Counters;
Clients see a type named Counter, a constant Zero, and
two subprograms. The fact that a counter is an integer in the range 0 to one
million stays in the private part. Even the value of Zero is
hidden: the visible declaration only promises that the constant exists.
package body Counters is
procedure Increment (C : in out Counter) is
begin
C := C + 1;
end Increment;
function Value (C : Counter) return Integer is
(Integer (C));
end Counters;
The body can use the full type, so it increments the counter with ordinary
integer arithmetic and converts it to Integer for the caller. The
conversion is legal here because the body sees that Counter is an
integer type; a client cannot perform it.
with Ada.Text_IO;
with Counters;
use Ada.Text_IO;
procedure Counter_Main is
C : Counters.Counter := Counters.Zero;
begin
for I in 1 .. 3 loop
Counters.Increment (C);
end loop;
Put_Line ("counter =" & Integer'Image (Counters.Value (C)));
end Counter_Main;
The main program creates a counter from the exported constant and changes it
only through Increment. It never needs to know that the counter is
stored as an integer, and the representation could change without touching this
code.
$ gnatmake -gnat2022 -gnatwa counter_main.adb $ ./counter_main counter = 3
A private type is opaque
Because the full type is hidden, a client cannot assign a number to a
Counter or convert one to Integer. The compiler treats
the private type as distinct from every visible type, and the error appears
where the client tries to cross the boundary.
with Ada.Text_IO; with Counters; use Ada.Text_IO; procedure Counter_Error is C : Counters.Counter := Counters.Zero; begin C := 5; Put_Line (Integer'Image (Counters.Value (C))); end Counter_Error;
The assignment to C is rejected even though the hidden type is an
integer type, and the diagnostic names the private type and the place where it
was declared. This is the intended protection: a client can use a counter but
cannot manufacture an arbitrary value for it.
$ gnatmake -gnat2022 -gnatwa counter_error.adb x86_64-linux-gnu-gcc-14 -c -gnat2022 -gnatwa counter_error.adb counter_error.adb:8:09: error: expected private type "Counter" defined at counters.ads:2 counter_error.adb:8:09: error: found type universal integer gnatmake: "counter_error.adb" compilation error
Package initialisation
A package body may have statements after its declarations, between
begin and end. They run once, when the package is
elaborated, before the main program's statements. This is the place for setting
up a table, opening a resource, or printing a banner that should appear exactly
once per run.
package Banner is procedure Show; end Banner;
The specification declares a single procedure and says nothing about initialisation. Callers do not need to know that the body prepares something when the program starts.
with Ada.Text_IO;
use Ada.Text_IO;
package body Banner is
procedure Show is
begin
Put_Line ("banner package is ready");
end Show;
begin
Put_Line ("banner package is elaborated");
end Banner;
The final begin block belongs to the package body, not to
Show. Its statement runs during elaboration, so it appears before
anything the main program prints.
with Ada.Text_IO;
with Banner;
use Ada.Text_IO;
procedure Banner_Main is
begin
Put_Line ("main program starts");
Banner.Show;
end Banner_Main;
The output order proves the point: the package message comes first, then the
first statement of Banner_Main, and finally the procedure call.
Elaboration happens once, no matter how many times Show is called.
$ gnatmake -gnat2022 -gnatwa banner_main.adb $ ./banner_main banner package is elaborated main program starts banner package is ready
Using and renaming packages
A use clause makes the declarations of a package directly visible,
so a call no longer needs the package name. A package renaming declares a
shorter local name for a package and is often used for Ada.Text_IO.
Both devices are local to the declarative region that contains them, and
qualification always remains available.
with Ada.Text_IO;
with Shapes;
procedure Names is
package IO renames Ada.Text_IO;
use Shapes;
begin
IO.Put_Line ("circle =" & Float'Image (Circle_Area (2.0)));
IO.Put_Line ("ring =" & Float'Image (Shapes.Ring_Area (3.0, 2.0)));
IO.Put_Line ("pi =" & Float'Image (Pi));
end Names;
After use Shapes, Circle_Area and Pi are
visible without a prefix, while Shapes.Ring_Area still works and
shows the qualified form. IO is a rename, not a copy: it denotes
the same package. If two used packages declared the same name, the unqualified
name would be ambiguous, and the qualified form would resolve it.
$ gnatmake -gnat2022 -gnatwa names.adb $ ./names circle = 1.25664E+01 ring = 1.57080E+01 pi = 3.14159E+00
use type
A with clause makes a package visible but does not put its
declarations into scope. That includes the primitive operations of a type
declared in the package: the arithmetic and comparison operators of
Distances.Meters belong to the package and are not directly
visible to a client. A use type clause makes the operations of one
type directly visible without importing every name in the package.
package Distances is type Meters is range 0 .. 1_000_000; end Distances;
The package declares one integer type with its own range. The operations that
belong to Meters, including + and =, are
declared in this package together with the type.
with Ada.Text_IO;
with Distances;
use Ada.Text_IO;
procedure Use_Type is
use type Distances.Meters;
A : Distances.Meters := 0;
begin
for I in 1 .. 25 loop
A := A + 5;
end loop;
if A = 125 then
Put_Line ("125 meters");
end if;
end Use_Type;
Inside Use_Type, use type Distances.Meters makes
+ and = available for that type, so the loop can
accumulate metres and the condition can compare the total with 125. The
package name is still needed for the type itself, and nothing else from
Distances becomes visible. The loop reaches 125 after twenty-five
steps of five metres.
$ gnatmake -gnat2022 -gnatwa use_type.adb $ ./use_type 125 meters
Without use type
Without the use type clause, even the addition is rejected. The
diagnostic points at the operator rather than at the package, and GNAT suggests
a use clause as the remedy.
with Ada.Text_IO; with Distances; use Ada.Text_IO; procedure No_Use is A : Distances.Meters := 100; begin A := A + 25; Put_Line (Distances.Meters'Image (A)); end No_Use;
The error is reported at the + in A + 25. Adding
use Distances would also work, but it would make every name in the
package directly visible; use type limits the effect to the
operators of one type.
$ gnatmake -gnat2022 -gnatwa no_use.adb x86_64-linux-gnu-gcc-14 -c -gnat2022 -gnatwa no_use.adb no_use.adb:8:11: error: operator for type "Meters" defined at distances.ads:2 is not directly visible no_use.adb:8:11: error: use clause would make operation legal gnatmake: "no_use.adb" compilation error
Child packages
A package can have child packages, written with a dot in the name. A child sees
the declarations of its parent without a with clause, which makes
it suitable for extending a package with related operations. The file name uses
a dash where the unit name uses a dot, so the child
Shapes.Circles lives in shapes-circles.ads and
shapes-circles.adb.
package Shapes.Circles is function Circumference (Radius : Float) return Float; end Shapes.Circles;
The specification of the child declares one new function. It does not repeat
Pi or Circle_Area; those names come from the parent.
package body Shapes.Circles is
function Circumference (Radius : Float) return Float is
(2.0 * Pi * Radius);
end Shapes.Circles;
The body uses Pi directly, because a child unit has visibility on
its parent's visible declarations. It could also call
Circle_Area, and it would see the parent's private part only if it
were declared as a private child.
with Ada.Text_IO;
with Shapes.Circles;
use Ada.Text_IO;
procedure Circles_Main is
Radius : constant Float := 3.0;
begin
Put_Line ("area =" & Float'Image (Shapes.Circle_Area (Radius)));
Put_Line ("circumference =" &
Float'Image (Shapes.Circles.Circumference (Radius)));
end Circles_Main;
The main program withs the child package, and through it also gets the parent.
With a radius of 3 the area is about 28.27 and the circumference about 18.85,
both computed from the same Pi. A child package can be added
without editing the parent's files.
$ gnatmake -gnat2022 -gnatwa circles_main.adb $ ./circles_main area = 2.82743E+01 circumference = 1.88495E+01
Next steps
The next article, strings, works with the predefined
String type and the library package Ada.Strings.Fixed.
The package in this article has shown how a specification, a body, and a
private part divide an interface from its implementation; the strings article
uses the same split when it calls library routines instead of writing them.
Source
In this article we have declared a package inside a program, moved it into a
specification and a body, hidden a type behind a private part, added
initialisation statements to a package body, used use clauses and
package renaming, made the operators of one type visible with
use type, and extended a package with a child unit. The
previous article covered the
subprograms these packages contain, and the next article applies the same
structure to strings.
Author
List all Ada tutorials.