Contents | Prev | Next | Index


Typed Constants

Canterbury Pascal supports the concept of typed constants. Typed constants are basically intialized variable declarations. As such, they can be used exactly like variables of the same type, except that they should not be modified.

const_decl        ::= const_def                              
                  ::= const_var_decl = typed_const
const_var_decl    ::= const_var_id : type                    
const_var_id      ::= ident                                  
typed_const       ::= primary                                
                  ::= array_const                            
                  ::= record_const                           
array_const       ::= ( const_list )              
const_list        ::= const_list , element_const             
                  ::= element_const                          
element_const     ::= typed_const                            
record_const      ::= ( field_const_list )        
field_const_list  ::= field_const_list ; field_const         
                  ::= field_const                            
field_const       ::= field_id : typed_const
::= <empty> field_id ::= <identifier>

A typed constant can be any primary expression, as long as it can be computed during compile time. A primary expression is part of an expression operand. Examples:

CONST
  Maximum : INTEGER = 9999;
  Factor : DOUBLE = -0.1;
  Breakchar : CHAR = #3;
  Heading : STRING = 'Section';
  NewLine : STRING = #13#10;
  Color : SET OF ( red, green, blue ) = [red];

A typed constant can also be an array constant. Examples:

TYPE
  TStatus = (Active, Passive, Waiting);
  TStatusMap = ARRAY [TStatus] OF STRING;
CONST
  StatStr : TStatusMap =
  ('Active', 'Passive', 'Waiting' );
  Digits : ARRAY [0..9] OF CHAR =
  '0123456789';

Finally, a typed constant can also be a record constant, as shown in the following examples:

TYPE
  TPoint = RECORD
    x, y : SINGLE;
  END;
  TVector = ARRAY [0..1] OF POINT;
  TMonth = (Jan,Feb,Mar,Apr,May,Jun,
            Jul,Aug,Sep,Oct,Nov,Dec);
  TDate = RECORD
    d : 1..31;
    m : TMonth;
    y : 1900..2099;
  END;
CONST
  Origin : TPoint =
    (x:0.0; y:0.0);
  Line : TVector =
    ((x:-3.1; y:1.5), (x:5.8; y:3.0));
  SomeDay : TDate =
    (d:2; m:Dec; y:1960);

Notice that, unlike Borland Object Pascal, Canterbury Pascal does not accept procedural or address constant value for typed constants for the reason that these cannot be resolved at compile time because of Java.


Contents | Prev | Next | Index

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