next up previous contents index search.gif
Next: 3. Types Up: 2. Constants Previous: 2.1 Ordinary constants

2.2 Typed constants

Typed constants serve to provide a program with initialized variables. Contrary to ordinary constants, they may be assigned to at run-time. The difference with normal variables is that their value is initialised when the program starts, whereas normal variables must be initialised explicitly.

Typed constant declaration

\begin{syntdiag}\setlength {\sdmidskip}{.5em}\sffamily\sloppy \synt{typed\ const...
...*: \synt{type} \lit*= \synt{typed\ constant} \lit*; \\
\end{rep} \end{syntdiag}

\begin{syntdiag}\setlength {\sdmidskip}{.5em}\sffamily\sloppy \synt{typed\ const...
...nt} \\
\synt{record\ constant} \\
\synt{procedural\ constant}
\)\end{syntdiag}
Given the declaration:

Const
  S : String = 'This is a typed constant string';
The following is a valid assignment:

 S := 'Result : '+Func;
Where Func is a function that returns a String. Typed constants also allow you to initialize arrays and records. For arrays, the initial elements must be specified, surrounded by round brackets, and separated by commas. The number of elements must be exactly the same as number of elements in the declaration of the type. As an example:

Const
  tt : array [1..3] of string[20] = ('ikke', 'gij', 'hij');
  ti : array [1..3] of Longint = (1,2,3);
For constant records, you should specify each element of the record, in the form Field : Value, separated by commas, and surrounded by round brackets. As an example:

Type
  Point = record
    X,Y : Real
    end;
Const
  Origin : Point = (X:0.0 , Y:0.0);
The order of the fields in a constant record needs to be the same as in the type declaration, otherwise you'll get a compile-time error.

root
1999-06-10