Pages

Monday, September 17, 2012

AdaTutor - More Records and Types

Record Discriminants and Record Variants

The definition of a record type can have discriminants, which have the same
form as formal parameters ("dummy arguments") of subprograms, except that the
mode is omitted. Default values may be supplied. For example,

Although objects of type Matrix can be rectangular, objects of type
Square_Matrix must be square. In declaring these objects, we use the same
syntax as a subprogram call, with either positional or named notation:

With the simplified version of type Text that we presented earlier, every
object of type Text occupied enough memory for the longest string we expected
to handle (e.g., 80 characters). With this version, each object of type Text
that we create can have just the Maximum_Length we need, and its effective
length Pos can vary from zero to that Maximum_Length. For example, we could
declare Name : Text(50); and Employee_Number : Text(9).

The definition of a record type can have a variant, which also has the same
form as a subprogram formal parameter without the mode. However, the syntax of
a case construct is used to specify part of the record. Although a record can
have several discriminants, it can have only one variant, and the variant part
must appear last in the record. For example,

If the sex of the person is Male, we want the record to include a Boolean
showing whether he's bearded, but if the sex is Female, we want the record to
include an Integer (subtype Natural), showing the number of children she has.