Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 36 ยท Types and Control Flow

Records, Discriminants and Aggregates in Ada

Group data with Ada records: components, default values, aggregates with named association, discriminants, variant records and how they compare with C structs and unions.

A record groups values of different types under one name, like a struct in C. Ada records add three things C lacks: default values for components, aggregates that build a whole record in one expression, and discriminants that make a record's shape depend on a value, with the compiler checking that you only touch components that exist.

Ada code is read-only; C stand-ins run on the site.

Declaring and using records

ADA
type Point is record
   X, Y : Float := 0.0;
end record;

type Employee is record
   Name   : String (1 .. 20);
   Age    : Natural;
   Salary : Float := 0.0;
end record;

P : Point;                          --  X = 0.0, Y = 0.0 by default
E : Employee;
begin
   E.Age := 36;
   E.Salary := 5200.0;

Components are selected with a dot. Defaults on the component declaration apply to every object created without an initial value, which removes a lot of uninitialized-data bugs.

Aggregates

An aggregate builds a complete record value:

ADA
P1 : Point := (X => 1.0, Y => 2.5);       --  named association (preferred)
P2 : Point := (3.0, 4.0);                 --  positional
P3 : Point := (X | Y => 7.0);             --  same value for two components

Named association is self-documenting and lets the compiler complain when you forget a component. Ada 2022 also supports delta aggregates to copy a record with a few changes: P1'Update (Y => 9.0).

Records can be compared with = and copied with :=; components can themselves be arrays or other records.

Nested and array-of-record data

ADA
type Rectangle is record
   Top_Left, Bottom_Right : Point;
end record;

Shapes : array (1 .. 10) of Rectangle := (others => ((0.0, 0.0), (1.0, 1.0)));
Shapes (3).Bottom_Right.X := 5.0;

Discriminants

A record may take discriminants, parameters that are fixed when the object is created:

ADA
type Buffer (Size : Natural) is record
   Data   : String (1 .. Size);
   Length : Natural := 0;
end record;

B1 : Buffer (Size => 16);
B2 : Buffer (Size => 256);

The array length is determined by the discriminant, so each Buffer is exactly as big as asked, with no pointer and no separate length field. B1.Size can be read but not assigned.

Variant records

A discriminant of an enumeration type can choose which components exist, like a checked union:

ADA
type Shape_Kind is (Circle, Rectangle, Triangle);

type Shape (Kind : Shape_Kind) is record
   case Kind is
      when Circle    => Radius : Float;
      when Rectangle => Width, Height : Float;
      when Triangle  => A, B, C : Float;
   end case;
end record;

S : Shape := (Kind => Circle, Radius => 2.0);

Reading S.Width when Kind is Circle raises Constraint_Error. In C the equivalent union lets you read the wrong member silently. Ada tracks which variant is active and checks every access.

flowchart TD
    S["Shape (Kind)"] --> C["Kind = Circle<br/>Radius"]
    S --> R["Kind = Rectangle<br/>Width, Height"]
    S --> T["Kind = Triangle<br/>A, B, C"]

Only the components of the active variant exist.

Stand-in: a checked variant record in C

C unions cannot protect you, so this stand-in uses a tagged union and an accessor that fails the way Ada raises Constraint_Error when you read a component from the wrong variant.

C Stand-in: variant record with checked access

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 shows record defaults and designated initialization, which is C's closest match to named aggregates.

C Stand-in: defaults and named aggregates

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

  • Positional aggregates with several components of the same type: the order is easy to mix up. Prefer named association.
  • Trying to assign a discriminant on its own; to change it you must assign a whole record value.
  • Forgetting that a variant record created without a default discriminant is a fixed-shape object.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is an aggregate in Ada?

    Aggregates such as (X => 1.0, Y => 2.0) create composite values in one expression.
  2. 2. What does a discriminant like Size in type Buffer (Size : Natural) provide?

    The discriminant parameterizes the record, for example giving the length of an array component.
  3. 3. What happens if you read S.Width when S.Kind = Circle in a variant record?

    Ada checks that the component belongs to the active variant, unlike a C union.
  4. 4. Which aggregate form is preferred for readability?

    Named association shows which value goes to which component and is robust against reordering.

Summary

  • Records group components with optional defaults and are selected with a dot.
  • Aggregates build values with named or positional association.
  • Discriminants parameterize records; variant records give checked, union-like alternatives.
  • Records support = and := and nest freely with arrays.

Next: If, Case and Loops