Ada Programming/Types
This page describes the various types available in Ada. If you want to know how to declare new types, how they relate to each other and how to convert them see Ada Programming/Subtypes
Types in Ada
If you look at Ada's list of keywords, you will notice that there is no int or INTEGER like for example with C/C++.
Ada has only a very small set of predefined types, rather new types are defined according to domain specific needs. Thus Ada is more like PL/I where data types are described in terms of what is needed and not chosen from a predefined set.
Unlike PL/I, the description of a data type is not done "low level" in terms of bits but "high level" using human readable values. If a low level specification is needed as well, optional representation clauses can be used.
Example:
type Day_Of_Month is range 1 .. 31;
for Day_Of_Month'Size use 8;
The two statements in detail:
- The type is defined as a range of whole numbers with lower bound 1 and upper bound 31. No values outside this range can be assigned to objects of this type. The type is also given the name "Day_Of_Month".
- Representation clause: use 8 bit of storage. If Size is not specified then the compiler will choose a suitable size.
You should not specify representation clauses if there is no pressing need (like e.g. interfacing to external devices). The more freedom the compiler has, the better code can be generated.
Strong typing
Objects of different types in Ada are incompatible, i.e. may not be assigned or mixed, even if they have matching value ranges.
type My_Integer is range -10 .. 10;
type Your_Integer is range -10 .. 10;
type His_Integer is new Your_Integer;
M: My_Integer;
Y: Your_Integer;
H: His_Integer;
M := Y; -- illegal
H := Y; -- illegal
This prevents inadvertent mixing of apples and oranges. If you really need to combine objects of different types, you have to use type conversions.
Should you need many type conversions, i.e. if Ada's strong typing gets into your way, this generally is an indication of bad program design. Consider using subtype instead.
Type classification
In the predefined package Standard, the most basic types are defined. As stated before, you normally do not use those types, rather define your own ones. This is the set of type classes you have at your convenience. Do follow the links to get more information.
- Signed Integers (int, INTEGER)
- Signed Integers are called range in Ada.
- Unsigned Integers (unsigned, CARDINAL)
- Unsigned Integers are called Modular Types. Apart from being unsigned they also have wrap-around functionality.
- Enumerations (enum, char, bool, BOOLEAN)
- Ada Enumeration types are a separate type family.
- Floating point (float, double, REAL)
- Floating point values are defined by the digits needed.
- Binary and Decimal Fixed Point (DECIMAL)
- Fixed point type are defined by their delta.
- Arrays ([], ARRAY [] OF, STRING)
- Arrays with compile-time determined and run-time determined size are supported.
- Records (struct, class, union, RECORD)
- Ada uses records for compound types and classes.
- Access (*, ^, POINTER TO)
- Ada's Access types may be more than just a simple memory address.
Predefined types
Are you missing specific types?
These types are predefined in the Standard package:
- Integer
- This type covers at least the range -2**15+1 .. +2**15-1 (RM 3.5.4 (21) (http://www.adaic.com/standards/95lrm/html/RM-3-5-4.html) (Annotated (http://www.adaic.com/standards/95aarm/html/AA-3-5-4.html))).
- Float
- There is only a very weak implementation requirement on this type (RM 3.5.7 (14) (http://www.adaic.com/standards/95lrm/html/RM-3-5-7.html) (Annotated (http://www.adaic.com/standards/95aarm/html/AA-3-5-7.html))).
- Character
- In Ada, characters are a special form of Enumerations. There are two predefined kinds of character types: 8-bit characters (called Character) and 16-bit characters (called Wide_Character). In Ada 2005, a 32-bit character type called Wide_Wide_Character will be added. You can also specify your own character types.
- String
- In Ada, strings are a special form of Arrays, namely arrays of characters. Since there are two kinds of characters, there are also two kinds of strings: String is an array of Character, Wide_String is an array of Wide_Character. For ease of treatment of strings, there are special packages in three variants, one for handling fixed length strings: Ada.Strings.Fixed; one for strings with varying lengths below a certain upper bound: Ada.Strings.Bounded; and one for strings of unbounded lengths: Ada.Strings.Unbounded (for the wide kinds, prefix each name with Wide_ or Wide_Wide_).
- Boolean
- A Boolean in Ada is an Enumeration of False and True.
See also
Wikibook
Ada Reference Manual
|