Contents | Prev | Next | Index


Declarations and Scope Rules

As in its predecessor Pascal, Modula-2 requires every identifier occuring in a program to be either introduced by a declaration or to be imported. The only exception from this are the standard identifiers which can be regarded as automatically imported. Declarations also specify certain permanent properties of an item, such as whether it is a constant, type, variable or procedure. The identifier is then used to refer to the associated item.

The scope of an item extends textually from the point of its declaration to the end of the block. That is, it is local within its module, procedure or record according to which its declaration belongs to. The visibility of an item also extends to nested scopes unless it is hidden there by another declaration of the same identifier. Here are the scope rules:

  1. No identifier may refer to more than one item within a given scope. That means an item cannot be declared twice in the in the same block.
  2. An item may only be referenced within its (possibly nested) scope.
  3. A type T of the form POINTER TO T1 (see pointer types) can be declared before the scope of T1. In this case, the declaration of T1 must follow in the same block to which T is local.
  4. If an identifier defined in module M1 is exported, the scope expands over the block which contains M1 (see modules). If M1 is a compilation unit, it extends to all those units which import M1 (see compilation units).
  5. Identifiers designating record fields (see record types) or type-bound procedures (see type-bound procedures) are valid only in record designators and in with statements referring to a variable of that record type.

An identifier may be qualified. In this case it is prefixed by another idenifier which designates the module (see modules or compilation units) in which the qualified identifier is defined. The prefix and the identifier are separated by a period.

qualident ::= qualident . <identifier>
          ::= <identifier>

Modula-2 also has a number of standard identifiers. These are automatically imported, and, though not being reserved words, should not be redeclared.


Contents | Prev | Next | Index

Canterbury Modula-2 for Java  (Last documentation update Feb 8, 2000)
Copyright © 1998 Mill Hill & Canterbury Corporation, Ltd. All rights reserved
Please send any comments or corrections to mhc@webcom.com