Section 6.3 - Creating Types and Subtypes

In Ada, a type is characterized by a set of values and a set of primitive operations. For example, the type Integer can be characterized by a set of values (..., -2, -1, 0, 1, 2, ...) and a set of primitive operations (+, -, *, /, etc.). We'll learn more about the phrase ``primitive operation'' later.

An object of a given type is a run-time entity that contains (has) a value of the type. For example, a variable named `Number_Of_Widgets' is an object; Number_Of_Widgets could be of the type Integer.

Ada lets you create your own types, and has a very rich set of capabilities for creating types with exactly the operations you want.

To create a new type, use a type declaration. A type declaration begins with the keyword type, followed by the name of the new type, the keyword is, and then a definition of the new type. Here's an example of a new type named Column which can only have integer values in the range 1 through 72, and another type called Row that has values 1 through 24:

 type Column is range 1 .. 72;
 type Row    is range 1 .. 24;

Ada permits types declarations in essentially the same places as variable declarations, but in real Ada programs type declarations are usually declared near the beginning of a package specification, followed by declarations of subprograms that use that type. Types are occasionally declared near the beginning of package bodies for types that are only used in the internal implementation of a package.

One very important difference between Ada and some other languages is that Ada considers types different even if they happen to be implemented the same way at a particular time. For example, an object of type Column can't be added with an object of type Row or Integer without some additional expressions, even though they may be implemented the same way in the (current) underlying system. Why? Because they have different types. Now, you could create such operations to allow them to be mixed, but these operations don't come automatically.

This prohibition of mixing types is often useful for catching errors, but sometimes it's not what you want. Beginning Ada programmers sometimes create too many different numeric types, turning simple programs into complicated ones. If two different types are closely related and it should be possible to mix the different types together, perhaps you have two related types instead of two independent types. What you probably need in that case is a subtype declaration.

A subtype is simply another name for an existing type that may have some additional constraints on it. For example, let's say you have a program that manipulates counts of many different kinds of things. You could have a base type called `Count', and subtypes to represent counts of different kinds of things. If there must be less than 100,000 things, and widgets must have less than 100 (while there's no specific limit for eggs), you could define these subtypes as follows:

 type Count is range 0 .. 99_999;
 subtype Widget_Count is Count range 0 .. 99;
 subtype Egg_Count is Count;

Subtypes can be declared in the same places that types can be declared.

One place I specifically recommend using subtypes instead of using many new numeric types are in real-time control or scientific software in which there are a large number of different units that are combined throughout the software. It is possible in Ada to define a new type called "Meters", another called "Meters_Squared", another called "Meters_Per_Second", and so on. To make this scheme work, however, you have to create multiply operations that take two "meters" and multiply them to produce a "Meters_Squared", and so on. For most real software of this kind, the effort to generate all possible combinations isn't worth the benefit. There are usually a large number of different units and they are combined in many different ways throughout the program.

Don't get the idea that creating new numeric types are always a bad thing, however; there are a number of places where creating a new type for numeric values is appropriate. Ada provides you with a number of tools; you need to decide which tool is appropriate for your task.


If you want to permit free mixing of two different kinds of numbers, should they be defined as subtypes or different types?

  1. subtypes
  2. types

You may also:

PREVIOUS Go back to the previous section

NEXT     Skip to the next section

OUTLINE  Go up to lesson 6 outline

David A. Wheeler (

The master copy of this file is at "".