Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 36 ยท Subprograms and Packages

Packages in Ada: Specification and Body

Organize Ada code into packages: the specification as the public contract, the body as the hidden implementation, initialization, package state, use clauses, renaming and the Ada standard library.

A package is Ada's unit of modularity: a named collection of types, constants, variables and subprograms. It has two parts. The specification (.ads) lists what other code may see; the body (.adb) contains the implementation and is invisible to clients. Because the compiler checks every client against the specification only, you can change a body without recompiling anything that depends on it.

Ada listings are read-only here; C stand-ins model the idea using header/source separation.

A package specification

ADA
--  counters.ads
package Counters is
   Max_Count : constant := 1_000;

   type Counter is range 0 .. Max_Count;

   procedure Reset (C : out Counter);
   procedure Increment (C : in out Counter);
   function Value (C : Counter) return Integer;
end Counters;

The specification is the contract: names, types and signatures. It contains no executable code. Everything declared here is visible to any unit that does with Counters;.

The package body

ADA
--  counters.adb
package body Counters is

   procedure Reset (C : out Counter) is
   begin
      C := 0;
   end Reset;

   procedure Increment (C : in out Counter) is
   begin
      if C < Counter'Last then
         C := C + 1;
      end if;
   end Increment;

   function Value (C : Counter) return Integer is
   begin
      return Integer (C);
   end Value;

end Counters;

Every subprogram declared in the specification must have a body here. The body may declare private helpers that never appear in the specification and cannot be called from outside, the Ada equivalent of static functions in C.

Using a package

ADA
with Ada.Text_IO; use Ada.Text_IO;
with Counters;

procedure Main is
   C : Counters.Counter;
begin
   Counters.Reset (C);
   Counters.Increment (C);
   Put_Line (Integer'Image (Counters.Value (C)));
end Main;

with makes the package visible. You name its entities with the prefix Counters., or add use Counters; to drop it. To avoid long prefixes without a blanket use, rename: package C renames Counters;. For operators on a type, use type Counters.Counter; brings only the operators into scope.

Package state and initialization

A package may hold variables, initialized when the program starts (elaboration):

ADA
package body Log is
   Messages : Natural := 0;             --  hidden package state

   procedure Add (Text : String) is
   begin
      Messages := Messages + 1;
      Put_Line (Natural'Image (Messages) & ": " & Text);
   end Add;

begin
   Put_Line ("Log started");            --  runs once at elaboration
end Log;

The optional begin ... end section at the end of a body runs once, when the package is elaborated. Prefer constructors over hidden state when possible, because global state makes testing and concurrency harder.

Why the split matters

flowchart LR
    S["counters.ads<br/>(specification)"] -->|"clients compile against"| C["client.adb"]
    S -->|"implemented by"| B["counters.adb<br/>(body)"]
    B -.->|"can change freely"| B

Clients depend only on the specification. Changing the body does not force them to recompile.

The specification is also readable on its own: it serves as documentation that the compiler checks.

The standard library

The Ada runtime ships with packages under the Ada hierarchy:

Package Purpose
Ada.Text_IO text input and output
Ada.Strings.Unbounded growable strings
Ada.Containers.Vectors dynamic arrays
Ada.Calendar, Ada.Real_Time time and clocks
Ada.Numerics.Elementary_Functions sqrt, sin, log
Ada.Command_Line arguments and exit status

All of them are used the same way: with the package, then call its entities.

Stand-in: a header and a source file

C separates interface and implementation with a header and a source file. In this stand-in, a single file holds both, but the "specification" part declares only what a client may use, and the "body" part keeps static helpers private. The program mirrors the Counters package.

C Stand-in: the Counters package in C

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The second stand-in models package-level state and the one-time elaboration block with a static variable and an init function.

C Stand-in: package state and elaboration

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Common mistakes

  • Declaring a subprogram in the specification but forgetting its body (the binder complains).
  • Putting heavy implementation details in the specification, which forces clients to recompile.
  • Overusing use so that you cannot tell where a name comes from.
  • Relying on package-level variables as globals.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a package specification contain?

    The specification declares what clients may use; the implementation belongs to the body.
  2. 2. What file extensions do GNAT's specification and body use?

    .ads is the specification and .adb the body.
  3. 3. How do you avoid writing Counters. before every name without a blanket use clause?

    A renaming declaration gives a short alias.
  4. 4. When does the optional begin ... end part of a package body run?

    It is the package's initialization sequence, run once during elaboration.

Summary

  • A package has a specification (public contract) and a body (hidden implementation).
  • Clients use with (and optionally use or a renaming); the specification is all they depend on.
  • Package bodies may hold private helpers and state, initialized once at elaboration.
  • The standard library is organized as packages under Ada and System.

Next: Private Types and Child Packages