4.1 Type System Overview

[ Table of Contents ] Chapter Overview ] Next ] [ Glossary/Index ]

The following table provides a high-level summary of Ada's type system, divided into three categories: built-in types, user-defined elementary types, and user-defined composite types. The latter two categories are used to construct user-defined types based on elements provided by the built-in (language-defined) types.

Built-in
Types


(declared
in package
Standard)

type Boolean An enumeration type with two values (True, False)
type Integer The whole numbers from Integer'First to Integer'Last
subtype Natural Integers from 0 to Integer'Last
subtype Positive Integers from 1 to Integer'Last
type Float The real numbers, see Numeric Types below
type Character An enumeration type with 256 values (Latin-1 Set)
type Wide_Character An enumeration type with 65,536 values (Unicode)
type String An array type with values of type Character
type Wide_String An array type with values of type Wide_Character

User-Defined Elementary Types

Enumeration Types Lists of distinct named values

Numeric Types

Access Types Values that point to (reference) other objects

User-Defined
Composite Types

Array Types Arrays of values, all having the same type
Record Types Sets of components that may have different types
Derived Types Types derived from other (parent) types
Tagged Types Records that have "tags" -- used in OOP
Protected Types Values that are protected from simultaneous access
Task Types Values that are tasks (concurrent program units)

Other useful terms are: discrete type, which is either an integer type or an enumeration type, and scalar type, which is a discrete type or a real type (floating-point or fixed-point type).

The Declaration of Types

User-defined types are declared in a standard way as follows:

General Form

type Subtype_Name is Type_Definition;

where the form of the type definition is different for each kind of type listed above. Examples of each form are provided in the following pages (except for tagged, protected and task types, which are covered in a later chapter). Note that the identifier following the word type is referred to as a "subtype name" rather than a "type name." There are technical reasons for this. We will simply say here that types don't have names while subtypes do have names, and each type is its own "first subtype" -- which has the name given in the type declaration. A subtype name is sometimes referred to as a subtype mark. Subtypes are discussed further in a later section.

The Declaration of Objects

There are several ways to declare an object of a given type or subtype, illustrated below.

General Form without Initialization
Variable_Name : Subtype_Name;
General Form with Initialization
Variable_Name : Subtype_Name := Value;
Constant Declaration
Constant_Name : constant Subtype_Name := Value;

An object must, of course, be declared before it can be used. And its type or subtype must either be built-in or declared before the object can be declared. (This rule is ignored when declaring a one-of-a-kind array object -- covered later in this chapter -- or when declaring a one-of-a-kind protected object or one-of-a-kind task -- covered in a later chapter. In these cases the items are considered to be of anonymous type.)

Type Conversion

The value of a variable or parameter can often be converted from one type to another by executing an assignment statement as shown below. The expressions on the right are known as explicit type conversions.

General Form
New_Value := New_Type(Old_Value);
Example
My_Integer := Integer(My_Float);

Qualified Expressions

If an expression can have more than one interpretation (For example, Silver may be a legal value of either type Color or type Metal.), a qualified expression may be written (such as Color'Silver) to resolve the ambiguity.

Related Topics

This entire chapter B.1 Package Standard

[ Back to top of pageNext ]