1.5

Subrange Types

It is often the case that a variable will assume values of a certain type within a specific interval only. This can be expressed by defining the variable to be of a subrange type according to the format

type T = min .. max
(1.9)

where min and max are the limits of the interval.

11 Examples

type year    = 1900 .. 1999
type letter  = ‘A’ .. ‘Z’
type digit   = ‘0’ .. ‘9’
type officer = lieutenant .. general

Given the variables

var y: year
var L: letter

the assignments y := 1973 and L := ‘W’ are permissible, but y := 1291 and L := ‘9’ are not. The legality of such assignments cannot be verified by a compiler unless the value to be assigned is denoted by a constant or a variable of the same type. However, the admissibility of assignments of the kind

y := i     and     L := c

where ii is of type integer and cc is of type char, can be checked only upon program execution. In practice, systems performing these checks have proved enormously valuable in program development. Their utilization of redundant information to detect possible errors is again a prime motivation for using high-level language.

↑ Up