modula-2 home

  Home  
  Tutorial  
  Win32 API  
  Reference  
  Projects  
 

 

Type declarations

Subrange types

You can create subranges of any signed integer, unsigned integer, character and enumeration type.  The subrange type restricts the values allowed for the type, but leaves the same operations applicable.

To specify a subrange type, use the following format:

[typename][expression..expression]

The two expressions must be constructed solely of constants.  The first expression specifies the lowest value of the subrange and the second specifies the highest value of the subrange.

If you do not specify typename, the two expressions must be compatible and the subrange must be a subrange of the type of the expressions.

If the expressions are both integer expressions, use the following rules to determine the type from which the subrange is derived:

  • If the lower bound is greater than or equal to zero:
    • if the upper bound is less than or equal to the maximum of type CARDINAL, the subrange will be of type CARDINAL.
    • otherwise, the type is LONGINT
  • If the lower bound is less than zero:

    • if both bounds are within the range of type INTEGER, the type is INTEGER.
    • otherwise, the type is LONGINT.

You can specify typename to override the above conventions or for documentary purposes.  If typename is specified, both expressions must be compatible with it.

Examples:

TYPE

SmallInt  = [-128..128];

SmallCard = [0..255];

UpperCaseLetters   = CHAR['A'..'Z']


Source:

  • Wirth N: Programming in Modula-2, 3rd ed. Springer Verlag, Berlin, 1985.
  • Stony Brook Modula-2 documentation. Used with permission. Note: Stony-Brook M2 offers an extended syntax with features not described here. Stony Brook M2 users are encouraged to visit the Stony Brook website and to consult the Stony Brook help system.