Ada scalar types and subtypes
last modified November 23, 2026
A scalar type has values that can be ordered and compared one by one: integers, real numbers, characters, and enumerations. Ada lets a program declare its own scalar types with a range, and it keeps those types distinct even when they share a representation. Subtypes add a constraint to an existing type without creating a new one. This article shows how ranges, enumerations, and conversions work together.
Integer types with a range
The declaration type Percentage is range 0 .. 100; creates a new
integer type whose values are exactly the numbers from 0 to 100. The type has
its own name, its own attributes 'First and 'Last, and
its own checks. A variable of that type behaves like an integer inside its
range, and 'Image converts a value to a string for printing.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Percentage_Demo is
type Percentage is range 0 .. 100;
Score : Percentage := 85;
begin
Put_Line ("score =" & Percentage'Image (Score));
Put_Line ("first =" & Percentage'Image (Percentage'First));
Put_Line ("last =" & Percentage'Image (Percentage'Last));
Score := Score + 10;
Put_Line ("after bonus =" & Percentage'Image (Score));
end Percentage_Demo;
The score starts at 85 and rises to 95, which is still inside the range.
Percentage'First is 0 and Percentage'Last is 100.
The literal 10 in the addition has the universal integer type and takes the
type of Score, so no conversion is needed. The range is part of
the type, not a comment or a convention: the compiler knows it and the run-time
system enforces it.
$ gnatmake -gnat2022 -gnatwa percentage_demo.adb $ ./percentage_demo score = 85 first = 0 last = 100 after bonus = 95
Range checks at run time
A value outside the declared range is not a value of the type, so an
assignment that would produce one is checked. When the compiler can see the
bad value while compiling, it warns and still builds the program; the failure
then happens when the statement runs. The exception name is
Constraint_Error, and the message points at the line that
performed the check.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Range_Error is
type Percentage is range 0 .. 100;
Score : Percentage := 85;
begin
Put_Line ("score =" & Percentage'Image (Score));
Score := Score + 50;
Put_Line ("after bonus =" & Percentage'Image (Score));
end Range_Error;
The first line is printed, then the assignment tries to store 135 in a
Percentage and the range check fails. GNAT reports the static
problem twice, once as a value outside the type and once as the run-time
consequence, because the addition of two known values can be evaluated before
the program starts. If the same calculation depended on input, only the
run-time check would remain.
$ gnatmake -gnat2022 -gnatwa range_error.adb x86_64-linux-gnu-gcc-14 -c -gnat2022 -gnatwa range_error.adb range_error.adb:9:19: warning: value not in range of type "Percentage" defined at line 5 [enabled by default] range_error.adb:9:19: warning: Constraint_Error will be raised at run time [enabled by default] x86_64-linux-gnu-gnatbind-14 -x range_error.ali x86_64-linux-gnu-gnatlink-14 range_error.ali $ ./range_error score = 85 raised CONSTRAINT_ERROR : range_error.adb:9 overflow check failed
Subtypes
A subtype adds a range constraint to an existing type without creating a new
type. The declaration subtype Small is Integer range -10 .. 10;
makes Small another name for integers in that interval, and a
value of Small can be used wherever an Integer is
expected. The predefined subtypes Natural and
Positive are declared the same way, with ranges starting at zero
and at one.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Subtypes is
subtype Small is Integer range -10 .. 10;
S : Small := 7;
Total : Integer := 100;
begin
Total := Total + S;
S := S - 9;
Put_Line ("total =" & Integer'Image (Total));
Put_Line ("small =" & Small'Image (S));
Put_Line ("natural first =" & Integer'Image (Natural'First));
Put_Line ("positive first =" & Integer'Image (Positive'First));
end Subtypes;
S is added directly to an Integer because both have
the same type; a subtype does not need a conversion. The subtraction leaves -2,
which is still a valid Small. Natural'First is 0 and
Positive'First is 1. Assigning 11 to S would fail the
same range check as before, even though the base type
Integer could hold it.
$ gnatmake -gnat2022 -gnatwa subtypes.adb $ ./subtypes total = 107 small =-2 natural first = 0 positive first = 1
Enumeration types
An enumeration type lists its values in order: type Color is (Red,
Green, Blue);. Each name becomes a constant of the new type, and the
position in the list gives the value its place in the ordering. The attributes
'First and 'Last give the ends of the list,
'Succ and 'Pred step to the next and previous value,
'Pos returns the zero-based position, and 'Val
returns the value at a position.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Colors is
type Color is (Red, Green, Blue);
C : Color := Red;
begin
C := Color'Succ (C);
Put_Line ("value =" & Color'Image (C));
Put_Line ("succ =" & Color'Image (Color'Succ (C)));
Put_Line ("pred =" & Color'Image (Color'Pred (C)));
Put_Line ("first =" & Color'Image (Color'First));
Put_Line ("last =" & Color'Image (Color'Last));
Put_Line ("pos of red =" & Integer'Image (Color'Pos (Red)));
Put_Line ("pos of blue =" & Integer'Image (Color'Pos (Blue)));
Put_Line ("at 2 =" & Color'Image (Color'Val (2)));
end Colors;
C starts as Red and becomes Green, whose
successor is Blue and whose predecessor is Red. The
positions confirm the order: Red is 0, Green is 1, and
Blue is 2, so Color'Val (2) is Blue.
Unlike Integer'Image, which writes a leading space for a
non-negative number, enumeration images contain no blank: they are the literal
names in upper case.
$ gnatmake -gnat2022 -gnatwa colors.adb $ ./colors value =GREEN succ =BLUE pred =RED first =RED last =BLUE pos of red = 0 pos of blue = 2 at 2 =BLUE
Distinct types are not interchangeable
Two integer types are different types even when they share a representation.
Meter and Foot below are both declared with range
constraints, but Ada does not let one stand for the other, because mixing them
silently is exactly the kind of mistake that a unit-bearing type is meant to
prevent. The assignment needs a conversion, and the compiler says so.
with Ada.Text_IO; use Ada.Text_IO; procedure Unit_Error is type Meter is range 0 .. 10_000; type Foot is range 0 .. 40_000; Distance : Meter := 1250; Length : Foot := Distance; begin Put_Line (Foot'Image (Length)); end Unit_Error;
The two error lines name the declared type of each side and the line of its
declaration. This is the same rule that forbids assigning an
Integer to a Float in the types
article; user-defined types make it visible for values that share a
representation. A comparison between the two types is not allowed either,
even though both are integer types.
$ gnatmake -gnat2022 -gnatwa unit_error.adb x86_64-linux-gnu-gcc-14 -c -gnat2022 -gnatwa unit_error.adb unit_error.adb:8:23: error: expected type "Foot" defined at line 6 unit_error.adb:8:23: error: found type "Meter" defined at line 5 gnatmake: "unit_error.adb" compilation error
Converting between scalar types
A conversion is written as the target type name followed by the value in
parentheses, as in Foot (Distance). The conversion changes the
type of the value and checks that the result lies in the target range; it does
not rescale the number, because Ada has no way to know that meters and feet
are related. Converting back restores the original value when the ranges
allow it.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Units is
type Meter is range 0 .. 10_000;
type Foot is range 0 .. 40_000;
Distance : Meter := 1;
begin
Distance := 1250;
Put_Line ("meters =" & Meter'Image (Distance));
Put_Line ("feet =" & Foot'Image (Foot (Distance)));
Put_Line ("back =" & Meter'Image (Meter (Foot (Distance))));
end Units;
All three lines print 1250, because a conversion only changes the type. A real
conversion between units would multiply by the factor and then convert the
result, for example Foot (Float (Distance) * 3.28084). If the
value were too large for the target range, the conversion would fail its range
check rather than wrap around.
$ gnatmake -gnat2022 -gnatwa units.adb $ ./units meters = 1250 feet = 1250 back = 1250
Next steps
The operators and expressions article shows the
comparisons and membership tests that these ordered types support. The next
article, attributes, takes a closer look at the apostrophe forms used
here, including 'Image, 'Value, 'Range,
and 'Size.
Source
In this article we have declared an integer type with a range, watched a range
check fail at run time, constrained an existing type with a subtype, used the
predefined Natural and Positive, walked through the
attributes of an enumeration type, seen two distinct types reject an implicit
assignment, and converted between them explicitly. The
previous article covers the expressions that
combine these values, and the next article examines the attributes in more
detail.
Author
List all Ada tutorials.