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;

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;

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


Quiz:


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 the outline of lesson 6

David A. Wheeler (wheeler@ida.org)