Contents | Prev | Next

5.9 Record types

Description

Record types define collections of values. A record type may be empty (i.e. define a collection with no values). Each member of a record type's collection is called a field and is identified by a field identifier, with the possible exception of the variant selector which sometimes does not have a field identifier. Each member in a record's collection has a particular type (which can be a record type).

Sometimes groups of fields in a record type are mutually exclusive, in which case you can use variant parts in the record type to specify mutually exclusive groups of fields (these mutually exclusive groups are called variants). Each variant part contains a variant selector, and the value of this variant selector determines which variant if any is active. It is an error to access a field of a variant which is not active.

Example

Here are some examples of valid record types:

    record end

The record type above is empty.

    record
      name : string;
      grade : integer;
    end

The record type above has two fields. The first field is identified by the field identifier name, and this field is of type string. The second field is identified by the field identifier grade, and this field is of type integer.

Given the type below

    type
       clothes = (shoe, pants, shirt);

the following record type is valid

    record
    price : real;
    case kind : clothes of
       shoe : ( size : integer; );
       pants : ( waist, length : integer );
       shirt : ( neck : integer; sleeve : integer )
    end

and contain seven fields

price, kind, size, waist, length, neck, and sleeve.

The fields price and kind are always present and the value of the field kind determine which of the variants if any is present. The variants are

Syntax

The syntax for defining new record types is given below:

(NOTE: for clarity some parts of the syntax are omitted, see Irie Pascal Grammar for the full syntax):

   record-type = 'record' field-list 'end'

   case-constant = ordinal-constant

   case-constant-list = case-specifier { ',' case-specifier }

   case-specifier = case-constant [ '..' case-constant ]

   domain-type = type-identifier

   field-list = [
         fixed-part ';' variant-part [ ';' ] |
         fixed-part [ ';' ] |
         variant-part [ ';' ] |
      ]

   fixed-part = record-section { ';' record-section }

   identifier-list = identifier { ',' identifier }

   new-ordinal-type = enumerated-type | subrange-type

   new-pointer-type = '^' domain-type | '@' domain-type

   new-structured-type =
      [ 'packed' ] array-type |
      [ 'packed' ] record-type |
      [ 'packed' ] set-type |
      [ 'packed' ] file-type |
      [ 'packed' ] list-type |
                         object-type |
                         string-type

   new-type = new-ordinal-type | new-structured-type | new-pointer-type

   record-section = identifier-list ':' type-denoter

   tag-field = identifier

   tag-type = ordinal-type-identifier

   type-denoter = type-identifier | new-type

   type-identifier = identifier

   variant = case-constant-list ':' '(' field-list ')'

   variant-body = variant-list [ [ ';' ] variant-part-completer ] | variant-part-completer

   variant-list = variant { ';' variant }

   variant-part = 'case' variant-selector 'of' variant-body

   variant-part-completer = ( 'otherwise' | 'else' ) ( field-list )

   variant-selector = [ tag-field ':' ] tag-type

Contents | Prev | Next