modula-2 home

  Win32 API  


Constant declarations

Constants are objects that have a specific value assigned at compile time. Constants cannot change during the execution of the program.

The format of the constant declaration is:

   CONST {constdec}

where constdec is:

   identifier = value;

The identifier is the name of the constant you are defining.

Simple constants

For simple types, value is an expression giving the value of the constant. The expression must be made up of constants only, so that the compiler can evaluate it at compile time.

The following are examples of simple constants:

      One = 1;
      Pi = 3.14159;
      PiOver2 = Pi / 2.0;
      Debug = TRUE;
      Message = "That's not right!";

Structured constants

You can also create constants of array and record types.  The value for these structured types consists of a constant constructor.

If a component is a structured type, the value for that component must also be a constant constructor.

When creating structured constant types, you must specify the type of the constant.

Array constant constructors take the following form:

   TypeName{element {, element}}

where element is

   constantValue [BY repeatValue]

Record constant constructors take the following form.

   TypeName{element {, element}}

where element is


where constantValue is a valid constant expression for the field type.

The following are examples of structured constants:

     rectype =
          A , B : INTEGER;
          C    : CHAR
     arraytype1 = ARRAY [1..2], [1..2] OF INTEGER;
     arraytype2 = ARRAY [1..2] OF rectype;
     arraytype3 = ARRAY [0..255] OF CARDINAL;
     rtc  = rectype{1, 2, '*'};
     atc1 = arraytype1{{1, 12},{1, 23}};
     atc2 = arraytype2{{1, 2, '*'}, rectype{1, 2, '*'}};
     atc3 = arraytype3{1, 2 BY 254, 2};

Note that when an element of a structured type is itself a structure it is optional to prefix the {with the type name of the structure if it has a type name. The type name is not necessary since the type of the constant to follow is known from the declaration of the type. This also allows you to have anonymous types as elements and still be able to create a constant.

Variant Record constants

If you are creating a constant of a record type that has variants, you must specify a value for the tag field of each variant in the record.  This is true even if the tag field is not named.

The fields following the tag field must comply with the types of the fields for the variant selected by the value of the tag field.

The following are examples of variant record constants:

   TYPE varrec =
        F1   : INTEGER;
           TRUE : X : REAL; |
           FALSE: Y : LONGINT;

     varrecf = varrec{1, FALSE, 12};
     varrect = varrec{1, TRUE, 1.5};

Set constants

Set constants have the following syntax:

   SetType {element {,element}}

element is:

   expression [..expression]

where expression is a compile time constant expression assignable to the base type of the set.

If the .. form is used, all elements within the range of the two expressions are included in the set.


   BITSET{1, 3..5}
   ColorSet{Red, Blue}


  • 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.