ZetCode

Ada tagged types

last modified November 23, 2026

A tagged type is a record with a run-time tag. The tag records which type the object actually has, and it allows a type to be extended with new components while operations written for the parent still accept the child. This article defines tagged records, declares primitive operations in a package specification, calls them with dot notation, extends a tagged type, and shows why a primitive has to be visible to the client. Dispatching on the tag is the subject of the next article.

A tagged record

The word tagged between is and record makes the type extensible and gives every object a tag. The primitive operations of the type are the subprograms declared in the same package specification, immediately after the type. Here the only operation is Discounted, written as an expression function that returns nine-tenths of the price.

catalog.ads
package Catalog is
   type Item is tagged record
      Name  : String (1 .. 8);
      Price : Integer := 0;
   end record;

   function Discounted (I : Item) return Integer is
     (I.Price - I.Price / 10);
end Catalog;

The package has only a specification. Item holds a fixed-length name and a price, and Discounted is its single primitive.

catalog_main.adb
with Ada.Text_IO;
with Catalog;
use Ada.Text_IO;

procedure Catalog_Main is
   Hammer : constant Catalog.Item :=
     (Name => "hammer  ", Price => 20);
begin
   Put_Line ("discounted =" & Integer'Image (Hammer.Discounted));
end Catalog_Main;

Hammer.Discounted is the dot notation for Discounted (Hammer). It is available because Discounted is a primitive of Item and is declared in the package specification, where the client can see it. The package has no body, because an expression function needs none. Integer division truncates the discount: 20 divided by 10 is 2, so the result is 18.

$ gnatmake -gnat2022 -gnatwa catalog_main.adb
$ ./catalog_main
discounted = 18

Primitive procedures and a body

A primitive can also be a procedure, and a procedure needs a body unless it is declared abstract. Show is declared in the specification and implemented in catalog.adb, where use Ada.Text_IO makes the output calls short. The client calls it with the same dot notation that it used for the function.

catalog.ads
package Catalog is
   type Item is tagged record
      Name  : String (1 .. 8);
      Price : Integer := 0;
   end record;

   function Discounted (I : Item) return Integer is
     (I.Price - I.Price / 10);

   procedure Show (I : Item);
end Catalog;

The specification now declares the procedure Show next to the expression function. Both are primitives of Item.

catalog.adb
with Ada.Text_IO;

package body Catalog is
   use Ada.Text_IO;

   procedure Show (I : Item) is
   begin
      Put (I.Name);
      Put_Line (Integer'Image (I.Price));
   end Show;
end Catalog;

The body implements Show. The package needs a body now, because a procedure declared in the specification must be completed somewhere.

catalog_main.adb
with Ada.Text_IO;
with Catalog;
use Ada.Text_IO;

procedure Catalog_Main is
   Hammer : constant Catalog.Item :=
     (Name => "hammer  ", Price => 20);
begin
   Hammer.Show;
   Put_Line ("discounted =" & Integer'Image (Hammer.Discounted));
end Catalog_Main;

The name is exactly eight characters, so Put (I.Name) writes hammer followed by two spaces. Show uses Put for the characters and Put_Line for the price, because Put_Line has no overload that takes a single Character. The expression function from the first version is still in the specification and still works.

$ gnatmake -gnat2022 -gnatwa catalog_main.adb
$ ./catalog_main
hammer   20
discounted = 18

Extending a tagged type

An extension adds components after the parent record. The syntax repeats the parent name after is new and gives the new components in a second record part. An aggregate for the extended type lists the inherited components and the new ones together, and the extension inherits every primitive of the parent.

catalog.ads
package Catalog is
   type Item is tagged record
      Name  : String (1 .. 8);
      Price : Integer := 0;
   end record;

   function Discounted (I : Item) return Integer is
     (I.Price - I.Price / 10);

   procedure Show (I : Item);

   type Stocked_Item is new Item with record
      Stock : Natural := 0;
   end record;

   procedure Restock (S : in out Stocked_Item; Amount : Natural);
   procedure Report (S : Stocked_Item);
end Catalog;

Stocked_Item extends Item with a Stock component and declares two primitives of its own.

catalog.adb
with Ada.Text_IO;

package body Catalog is
   use Ada.Text_IO;

   procedure Show (I : Item) is
   begin
      Put (I.Name);
      Put_Line (Integer'Image (I.Price));
   end Show;

   procedure Restock (S : in out Stocked_Item; Amount : Natural) is
   begin
      S.Stock := S.Stock + Amount;
   end Restock;

   procedure Report (S : Stocked_Item) is
   begin
      Put (S.Name);
      Put (Integer'Image (S.Price));
      Put_Line (" stock =" & Natural'Image (S.Stock));
   end Report;
end Catalog;

The body implements the new primitives. Report reads the inherited Name and Price as if they were declared in the extension itself.

catalog_main.adb
with Ada.Text_IO;
with Catalog;
use Ada.Text_IO;

procedure Catalog_Main is
   Saw  : Catalog.Stocked_Item :=
     (Name => "saw     ", Price => 12, Stock => 3);
   Copy : constant Catalog.Item := Catalog.Item (Saw);
begin
   Saw.Restock (5);
   Saw.Report;
   Copy.Show;
   Put_Line ("discounted =" & Integer'Image (Saw.Discounted));
end Catalog_Main;

Restock and Report are new primitives of Stocked_Item, while Show and Discounted are inherited from Item. The conversion Catalog.Item (Saw) produces a view of the parent part, and assigning that view to Copy copies only the name and the price. The stock belongs to the extension and is not part of the parent, so Copy.Show prints saw 12 without it.

$ gnatmake -gnat2022 -gnatwa catalog_main.adb
$ ./catalog_main
saw      12 stock = 8
saw      12
discounted = 11

Primitives belong in the specification

Only a subprogram declared in the same package specification as the type is a primitive operation that clients can select. A procedure placed in the package body is an ordinary local subprogram: the body can call it, but the client cannot, and the compiler does not treat it as an operation of the type. The program below makes that mistake on purpose.

hidden.ads
package Hidden is
   type Box is tagged record
      Value : Natural := 0;
   end record;
end Hidden;

The specification declares the tagged type Box and no operations.

hidden.adb
package body Hidden is
   procedure Bump (B : in out Box) is
   begin
      B.Value := B.Value + 1;
   end Bump;
end Hidden;

The procedure Bump appears only in the body, so it is hidden from clients of the package.

hidden_main.adb
with Hidden;

procedure Hidden_Main is
   B : Hidden.Box;
begin
   B.Bump;
end Hidden_Main;

The compiler rejects B.Bump because no selector with that name is visible for Box. Declaring Bump in hidden.ads would make it a primitive and make the call legal; the package body would then contain only its implementation. This is why a package that exports a tagged type usually declares all of its operations in the specification, even the ones that seem internal.

$ gnatmake -gnat2022 -gnatwa hidden_main.adb
x86_64-linux-gnu-gcc-14 -c -gnat2022 -gnatwa hidden_main.adb
hidden_main.adb:6:05: error: no selector "Bump" for type "Box" defined at hidden.ads:2
gnatmake: "hidden_main.adb" compilation error

An applied example: accounts

The final example models two kinds of bank account. Account has an owner and a balance, with operations to deposit, withdraw, and show the balance. Savings extends it with an interest rate and adds Add_Interest. Both objects use the inherited Show, and the savings object also uses the operation that belongs to its own type.

accounts.ads
package Accounts is
   type Account is tagged record
      Owner   : String (1 .. 8);
      Balance : Natural := 0;
   end record;

   procedure Deposit (A : in out Account; Amount : Natural);
   procedure Withdraw (A : in out Account; Amount : Natural);
   procedure Show (A : Account);

   type Savings is new Account with record
      Rate : Natural := 3;
   end record;

   procedure Add_Interest (S : in out Savings);
end Accounts;

Account has three primitives, and Savings inherits them and adds Add_Interest.

accounts.adb
with Ada.Text_IO;

package body Accounts is
   use Ada.Text_IO;

   procedure Deposit (A : in out Account; Amount : Natural) is
   begin
      A.Balance := A.Balance + Amount;
   end Deposit;

   procedure Withdraw (A : in out Account; Amount : Natural) is
   begin
      if Amount > A.Balance then
         raise Constraint_Error with "insufficient funds";
      end if;
      A.Balance := A.Balance - Amount;
   end Withdraw;

   procedure Show (A : Account) is
   begin
      Put (A.Owner);
      Put_Line (Natural'Image (A.Balance));
   end Show;

   procedure Add_Interest (S : in out Savings) is
   begin
      S.Balance := S.Balance + S.Balance * S.Rate / 100;
   end Add_Interest;
end Accounts;

The body implements the four procedures. Withdraw refuses an amount larger than the balance, and Add_Interest applies the rate as a whole-number percentage.

accounts_main.adb
with Ada.Text_IO;
with Accounts;
use Ada.Text_IO;

procedure Accounts_Main is
   Alice : Accounts.Account :=
     (Owner => "alice   ", Balance => 100);
   Bob : Accounts.Savings :=
     (Owner => "bob     ", Balance => 200, Rate => 5);
begin
   Alice.Deposit (50);
   Alice.Withdraw (30);
   Bob.Add_Interest;
   Alice.Show;
   Bob.Show;
   Put_Line ("bob rate =" & Natural'Image (Bob.Rate));
end Accounts_Main;

Alice ends with 120 after a deposit of 50 and a withdrawal of 30. Bob starts with 200, and five percent interest adds 10, for 210. Bob.Show calls the inherited operation, which knows only about the parent components, so the rate is printed separately. A withdrawal larger than the balance raises Constraint_Error with the message insufficient funds, which a caller can catch like any other exception.

$ gnatmake -gnat2022 -gnatwa accounts_main.adb
$ ./accounts_main
alice    120
bob      210
bob rate = 5

Next steps

The next article uses the tag for dispatching. It overrides a primitive in an extension, calls operations through a class-wide type, and introduces abstract types and interfaces. Those features build directly on the extension and the primitive operations shown here.

Source

Ada Reference Manual

GNAT User's Guide

AdaCore learning resources

In this article we have defined tagged records, declared primitive functions and procedures in a package specification, called them with dot notation, extended a type with a new component and new operations, converted an object to its parent type, and seen why an operation in a package body is not visible as a selector. The records article explains the components that a tagged record inherits from ordinary records.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Ada tutorials.