Contents | Prev | Next | Index


Subrange Types

A type T may be defined as a subrange of another basic type (except real types) or of another enumeration type by the specification of the lowest and the highest value in the subrange.

subrange_type ::= const_expr .. const_expr
const_expr    ::= expr

The first constant defines the lower bound, and it must not be greater than the defined upper bound. The resulting type T1 of the bounds is known as the base type or host type of T, and all operators applicable to operands of type T1 are also applicable to operands of type T. However, if a value is assigned to a variable of type T1, it must lie within the subrange interval. Both constants must be of the same host type. A variable of a subrange type T1 has all the properties of variables of the host type otherwise.

One syntactic ambiguity arises from allowing constant expressions whereas Pascal used to allow only simple constants. For example, given the following declarations:

CONST
  x = 50;
  y = 10;
TYPE
  Color = (Red, Green, Blue);
  Scale = (x-y) * 2 .. (x+y) * 2;

Standard Pascal syntax dictates that, if a type definition starts with a parenthesis, it's an enumerated type, such as Color in above example. However, the intent of the declaration of Scale is to define a subrange type. The solution is to reorganize the first subrange expression so that it doesn't start with a parenthesis, or to set another constant equal to the value of the expression and use that constant in the type definition:

TYPE
  Scale = 2 * (x-y) .. (x+y) * 2;

Examples of subrange types:

0 .. N-1
"A" .. "Z"
Monday .. Friday


Contents | Prev | Next | Index

Canterbury Pascal for JVM  (Last documentation update Sep 02, 2004)
Copyright © 1999-2004 J.Neuhoff - mhccorp.com  . All rights reserved.
Please send any comments or corrections to neuhoff@mhccorp.com