Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 36 ยท Subprograms and Packages

Private Types, Limited Types and Child Packages in Ada

Hide representation in Ada with private and limited private types, understand opaque handles, build hierarchies with child packages and private children, and keep invariants safe.

Packages give you modules, but real encapsulation needs something more: a way to publish a type's name and operations while hiding its structure. Ada provides private types for exactly this. Combined with child packages, they let you grow a library into a hierarchy without breaking the hiding.

Ada listings are read-only; C stand-ins model opaque handles.

Private types

ADA
package Stacks is
   type Stack is private;
   Empty_Stack : constant Stack;

   procedure Push (S : in out Stack; Item : Integer);
   procedure Pop  (S : in out Stack; Item : out Integer);
   function  Is_Empty (S : Stack) return Boolean;

private
   Capacity : constant := 100;
   type Int_Array is array (1 .. Capacity) of Integer;

   type Stack is record
      Items : Int_Array := (others => 0);
      Top   : Natural   := 0;
   end record;

   Empty_Stack : constant Stack := (Items => (others => 0), Top => 0);
end Stacks;

Everything before the word private is the public view: clients know there is a type Stack and can copy it, compare it with =, and pass it to Push and Pop. They cannot see Items or Top, so they cannot corrupt the stack by setting Top to 500. Everything after private is the full declaration, visible to the package body and its children but not to clients. Changing the representation later does not affect any client code, though clients recompile in the Ada model.

Limited types

A limited type forbids copying and comparison with =. Use it when a copy would be wrong: a file handle, a lock, a device:

ADA
type File_Handle is limited private;

Assignment A := B; is a compile error for a limited type, so two variables can never accidentally share one resource. You create limited objects with a function that returns an aggregate (Ada builds it in place) or through an initialization procedure. Many standard types, such as tasks and protected objects, are limited.

Declaration Client can copy? Client can compare? Client sees fields?
type T is private; yes yes no
type T is limited private; no no no
type T is record ... end record; yes yes yes

Child packages

You extend a package with a child, whose name includes the parent:

ADA
--  stacks-utils.ads
package Stacks.Utils is
   function Size (S : Stack) return Natural;
end Stacks.Utils;

The file is stacks-utils.ads. A child sees the private part of its parent, so it can use S.Top even though ordinary clients cannot. That lets you split a large package into files while sharing the secret representation among trusted units.

Children come in three flavors:

  • Public children (package Parent.Child): visible to everyone who withs them.
  • Private children (private package Parent.Helpers): usable only inside the parent hierarchy. They hold internal helpers.
  • Generic children: parameterized (see generics).
flowchart TD
    P["Stacks<br/>(public view + private part)"] --> U["Stacks.Utils<br/>(public child, sees the private part)"]
    P --> H["private Stacks.Helpers<br/>(internal only)"]
    C["Client"] -->|"with Stacks, Stacks.Utils"| P
    C -.->|"cannot with"| H

A child sees its parent's secrets; an outsider sees only the public view.

The Ada.Text_IO.Integer_IO family and Ada.Strings.Unbounded are examples of the same hierarchy idea.

Invariants through hiding

Because clients cannot reach Top, the package can promise "Top is always between 0 and Capacity". Every operation preserves it, and only the package must be reviewed to guarantee it. Ada 2012 lets you state this in the code (a Type_Invariant aspect, see contracts).

Stand-in: an opaque handle in C

C's closest tool is the opaque pointer: the header declares typedef struct Stack Stack; and only the source file knows the fields. The stand-in below puts the "client" in main and keeps the struct layout to the "body" section. A copy of the handle would share the one stack, which is why C has no way to stop the aliasing a limited type prevents.

C Stand-in: opaque stack handle

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.

A second stand-in checks an invariant that only the package's own operations may break, the reason hiding the representation matters.

C Stand-in: an invariant protected by hiding

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

  • Making types public records "for convenience" and then being unable to change them.
  • Forgetting that limited types cannot be assigned or compared with =.
  • Putting unrelated helpers into the specification instead of a private child.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What can a client do with type Stack is private;?

    A private type exposes its name and assignment and equality, but not its structure.
  2. 2. What does limited add?

    Limited types cannot be copied or compared, which is right for resources such as handles.
  3. 3. What can a child package Stacks.Utils see that an ordinary client cannot?

    Children can see their parent's private part.
  4. 4. What is a private child package for?

    A private package child can be withed only by units within the parent's hierarchy.

Summary

  • private hides a type's structure while exposing its name and operations; limited private also forbids copying.
  • The full declaration lives in the private part of the specification.
  • Child packages extend a package, see its private part, and can themselves be private.
  • Hiding is what lets a package guarantee invariants.

Next: Overloading, Nested Subprograms and Recursion