Structured type constructors allow programmers to define as new types complex information structures with components of the same or different types. They are:
The syntax of the type declaration is:
<type identifier> = ARRAY[ni1 .. nf1] OF <component type>;
for unidimensional arrays, or
<type identifier> = ARRAY[ni1 .. nf1, ni2 .. nf2, ...] OF <component type>;
for multidimensional arrays. Note that GLIDER restricts the array declaration syntax of index types to subranges, whereas Pascal accepts any ordinal type specification.
The first index can take values from ni1 to nf1, the second from ni2 to nf2, etc. The values of the indexes may be of type INTEGER, LONGINT, BYTE, WORD or enumerated type.
Examples:
TYPE Vec =ARRAY[-5, 5] OF REAL; VAR X,Z:Vec; Mat:ARRAY[1..3, 2..3] OF CHAR; ShirtPrice:ARRAY[Small. .XLarge, Cotton.. Silk] OF REAL;
Vec
will be a simple array with index values from -5 to 5.Mat
a two index array, the first from 1 to 3, the second
from 2 to 3. The successive elements in the memory will be:Mat[1, 2]; Mat[1, 3]; Mat[2, 2]; Mat[2, 3]; Mat[3, 2]; Mat[3, 3];
The rightmost indexes change first.
ShirtPrice
has, as indexes, values of an enumerated type
that have to be defined previously, for example with declarations:
Size = (Small, Medium, Large, XLarge); Fabric = (Dacron, Cotton, Flax, Silk);
The syntax of the declaration is:
<type identifier> = RECORD <list of field declarations> END;
The field declarations of the list are separated by ;
Examples:
PShip: ^Ship; Ship = RECORD Draft, Displacement: REAL; LoadClass: ARRAY[1..5]; Pfs: PShip; (*Pointer to the next ship*) END; TYPE Mo = (January, February, March, April, May, June, July, August, September, October, November, December); DayType = ( 1 .. 31); VAR Birthday: RECORD Month: Mo; Day: DayType; Year: WORD; END Bounty: Ship; PConvoy: PShip;The
Ship
record type has a pointer type field for building lists
of records of this type. In VAR
a type
record variable Bounty
is defined. The pointer variable PConvoy
can be used to construct
a list of records of type Ship.
The record variable Birthday
has fields type Mo
with
the values of the
months and Day
whose values range from 1 to 31. The field
Year
is of the predefined type WORD
, for this reason,
it can take values from 1 to 65535.
Fields of a record type variable are referred to by the variable name followed by the character, followed by the field name.
Example:
Birthday.Year
refers to the value of the field Year in the variable Birthday.
The syntax is:
<file type identifier> = FILE OF <type identifier>
<type identifier>
must not be another file type
identifier or any structured type identifier with a file type
identifier as component type.
Example:
FileShip: FILE OF Ship;The Pascal predefined type
TEXT
is a file of CHAR type for which Pascal
has special variable processing.Values of a set type are sets that can be operated with the set operators
SPMamp
+, -, *&
Also are defined the logical binary IN
operator relating
a base type value with a SET
type value and the set constructor
using the characters [ and ] as delimiters. The empty set is the
expression: [ ].
The syntax is:
<name> = SET OF <ordered type>;
Examples:
TYPE COLOR = SET OF (Red, Yellow, Green, Brown); VAR Paint, Hue, Dye: COLOR; Digit: SET OF 0 .. 9;
Paint
can take as value any of the 16 subsets of the elements
indicated in the declaration of its type (including the empty
and the total set).Digit
may take as value any subset of the
digits. One assignation of value may be :Digit: = [0, 1, 2];
2 IN Digit
Is true.
4 IN Digit
is false.If Paint := [Red, Yellow] and Hue := [Red, Green, Yellow]
SPMamp
Dye := Hue * Paint + [Brown]& would have the
elements Red
and Brown
.