[ Table of Contents ]
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 |
|
||||||||||||||||||
User-Defined Elementary Types |
|
||||||||||||||||||
User-Defined |
|
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 page ]