ZetCode

Ada discriminated and variant records

last modified November 23, 2026

A discriminated record carries a component, the discriminant, that controls the rest of the record. In its simplest use the discriminant sets the size of a string component, so one type describes buffers of different lengths. A record may also have a variant part, where a case on the discriminant selects which components exist. This article shows both forms, how objects are constrained by a discriminant, and how a whole-record assignment can change it.

A discriminant fixes a size

The discriminant is written in the type declaration, after the type name, in the same form as a parameter. Here Size is a Positive discriminant, and the component Data is a string whose upper bound is Size. Because the bounds depend on the discriminant, the type itself is unconstrained and each object must choose a size.

buffer_demo.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Buffer_Demo is
   type Buffer (Size : Positive) is record
      Data : String (1 .. Size);
   end record;

   B : constant Buffer (5) := (Size => 5, Data => "hello");
begin
   Put_Line (B.Data);
   Put_Line ("size =" & Positive'Image (B.Size));
end Buffer_Demo;

Buffer (5) is a constrained subtype of Buffer, and B has exactly five characters of storage. The aggregate states the discriminant and the data, and both values agree: a five-character string for a buffer of size 5. The discriminant can be read like any other component, but it is not an ordinary variable that can be assigned on its own.

$ gnatmake -gnat2022 -gnatwa buffer_demo.adb
$ ./buffer_demo
hello
size = 5

Different sizes in one type

Several objects of the same discriminated type may have different discriminants. A subtype can also fix the size once for a family of objects. The compiler tracks the constraint, so an object declared as Buffer (3) always has exactly three characters, no matter which value is assigned to it later.

buffer_sizes.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Buffer_Sizes is
   type Buffer (Size : Positive) is record
      Data : String (1 .. Size);
   end record;

   subtype Small_Buffer is Buffer (3);

   A : constant Small_Buffer := (Size => 3, Data => "abc");
   B : constant Buffer (5) := (Size => 5, Data => "abcde");
begin
   Put_Line (A.Data);
   Put_Line (B.Data);
   Put_Line ("a size =" & Positive'Image (A.Size));
   Put_Line ("b size =" & Positive'Image (B.Size));
end Buffer_Sizes;

A and B belong to the same type Buffer but have different constraints, and each prints its own data and size. Assigning B to A would be accepted by the compiler, because the types match, but the run-time check would raise Constraint_Error: a five-character value cannot fit into a buffer whose discriminant is fixed at 3. GNAT warns about the mismatch when it can see it.

$ gnatmake -gnat2022 -gnatwa buffer_sizes.adb
$ ./buffer_sizes
abc
abcde
a size = 3
b size = 5

Variant parts

A variant part is a case inside the record, selecting on the discriminant. Each alternative declares the components that exist only for that value. Reading a component from the wrong alternative is a constraint error, which is the safety advantage over a record that stores every field for every kind of value.

shape_demo.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Shape_Demo is
   type Shape_Kind is (Circle, Rectangle);

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

   C : constant Shape (Circle) := (Kind => Circle, Radius => 2.0);
begin
   Put_Line ("kind =" & Shape_Kind'Image (C.Kind));
   Put_Line ("radius =" & Float'Image (C.Radius));
end Shape_Demo;

C is constrained to Circle, so it has a Radius and no Width or Height. The aggregate lists only the components of the active alternative. Reading C.Width would raise Constraint_Error, because the discriminant says that the object is a circle.

$ gnatmake -gnat2022 -gnatwa shape_demo.adb
$ ./shape_demo
kind =CIRCLE
radius = 2.00000E+00

Changing the discriminant

An object whose discriminant is not fixed may change it, but only through a whole-record assignment; a direct assignment to the discriminant is illegal. To allow an object without an initial value, the type must give the discriminant a default. The object is then unconstrained from the start, so a later aggregate may select another variant.

mutable.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Mutable is
   type Shape_Kind is (Circle, Rectangle);

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

   S : Shape;
begin
   S := (Kind => Rectangle, Width => 3.0, Height => 2.0);
   Put_Line ("kind =" & Shape_Kind'Image (S.Kind));
   Put_Line ("width =" & Float'Image (S.Width));

   S := (Kind => Circle, Radius => 1.5);
   Put_Line ("kind =" & Shape_Kind'Image (S.Kind));
   Put_Line ("radius =" & Float'Image (S.Radius));
end Mutable;

The first assignment makes S a rectangle; the second replaces the whole value and makes it a circle. Without the default value, S : Shape; would be rejected with "unconstrained subtype not allowed (need initialization)", because the compiler has no discriminant to give the object. With the default, the declaration is legal, and each assignment replaces the discriminant together with the variant components. Writing S.Kind := Rectangle; would fail with "assignment to discriminant not allowed".

$ gnatmake -gnat2022 -gnatwa mutable.adb
$ ./mutable
kind =RECTANGLE
width = 3.00000E+00
kind =CIRCLE
radius = 1.50000E+00

A case over the discriminant

A subprogram parameter of an unconstrained discriminated type accepts any variant. Inside, a case statement on the discriminant selects the branch, and within each branch the components of that alternative are available. This is the usual way to give each variant its own behaviour without testing for errors.

area_demo.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Area_Demo is
   type Shape_Kind is (Circle, Rectangle);

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

   function Area (S : Shape) return Float is
   begin
      case S.Kind is
         when Circle =>
            return 3.14159 * S.Radius * S.Radius;
         when Rectangle =>
            return S.Width * S.Height;
      end case;
   end Area;

   C : constant Shape := (Kind => Circle, Radius => 2.0);
   R : constant Shape := (Kind => Rectangle, Width => 3.0, Height => 2.0);
begin
   Put_Line ("circle area =" & Float'Image (Area (C)));
   Put_Line ("rectangle area =" & Float'Image (Area (R)));
end Area_Demo;

The parameter S : Shape is unconstrained, so both calls fit. The compiler knows that S.Radius is valid in the Circle branch and that S.Width and S.Height are valid in the Rectangle branch. The circle has area 12.5664 and the rectangle has area 6.

$ gnatmake -gnat2022 -gnatwa area_demo.adb
$ ./area_demo
circle area = 1.25664E+01
rectangle area = 6.00000E+00

An applied example: a value of several types

Variant records can hold a value that may be an integer, a real, or a Boolean, with the discriminant recording which one is present. A procedure then uses a case on the discriminant to print the active component in the appropriate format. The same pattern appears in interpreters and message formats, where one field has several possible representations.

value_demo.adb
with Ada.Text_IO;
use Ada.Text_IO;

procedure Value_Demo is
   type Value_Kind is (Int_Value, Real_Value, Bool_Value);

   type Value (Kind : Value_Kind) is record
      case Kind is
         when Int_Value =>
            I : Integer;
         when Real_Value =>
            R : Float;
         when Bool_Value =>
            B : Boolean;
      end case;
   end record;

   procedure Show (V : Value) is
   begin
      case V.Kind is
         when Int_Value =>
            Put_Line ("integer" & Integer'Image (V.I));
         when Real_Value =>
            Put_Line ("real" & Float'Image (V.R));
         when Bool_Value =>
            Put_Line ("boolean " & Boolean'Image (V.B));
      end case;
   end Show;

   V1 : constant Value := (Kind => Int_Value, I => 7);
   V2 : constant Value := (Kind => Real_Value, R => 2.5);
   V3 : constant Value := (Kind => Bool_Value, B => True);
begin
   Show (V1);
   Show (V2);
   Show (V3);
end Value_Demo;

Each object states its discriminant and only the matching component, so no space is reserved for the other two values. Show reads V.I only in the integer branch, V.R only in the real branch, and V.B only in the Boolean branch. The output labels make the active kind visible, and the space after boolean keeps the label separate from the image of the Boolean value.

$ gnatmake -gnat2022 -gnatwa value_demo.adb
$ ./value_demo
integer 7
real 2.50000E+00
boolean TRUE

Next steps

The next article in the series covers access types, which point at objects created at run time and make it possible to build linked structures. Exceptions, including the Constraint_Error raised by a wrong variant or an out-of-range index, are covered after that.

Source

Ada Reference Manual

GNAT User's Guide

AdaCore learning resources

In this article we have declared discriminants that size a string component, constrained objects and subtypes by discriminant, declared a variant part selected by case, changed the discriminant with whole-record assignments, branched on it inside a function and a procedure, and stored values of three different types in one record type. The records article introduces the components and aggregates that these variants extend.

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.