Subsections

2 Constants

Just as in Turbo Pascal, Free Pascal supports both normal and typed constants.

1 Ordinary constants

Ordinary constants declarations are not different from the Turbo Pascal or Delphi implementation.

Constant declaration

\begin{syntdiag}\setlength{\sdmidskip}{.5em}\sffamily\sloppy \synt{constant\ dec...
...
\synt{identifier} \lit*= \synt{expression} \lit*; \\
\end{rep} \end{syntdiag}
The compiler must be able to evaluate the expression in a constant declaration at compile time. This means that most of the functions in the Run-Time library cannot be used in a constant declaration. Operators such as +, -, *, /, not, and, or, div(), mod(), ord(), chr(), sizeof can be used, however. For more information on expressions, see chapter Expressions. Only constants of the following types can be declared: Ordinal types, Real types, Char, and String. The following are all valid constant declarations:
Const
  e = 2.7182818;  { Real type constant. }
  a = 2;          { Ordinal (Integer) type constant. }
  c = '4';        { Character type constant. }
  s = 'This is a constant string'; {String type constant.}
  s = chr(32)
  ls = SizeOf(Longint);
Assigning a value to an ordinary constant is not permitted. Thus, given the previous declaration, the following will result in a compiler error:
  s := 'some other string';

2 Typed constants

Typed constants serve to provide a program with initialised 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\ consta...
...*: \synt{type} \lit*= \synt{typed\ constant} \lit*; \\
\end{rep} \end{syntdiag}

\begin{syntdiag}\setlength{\sdmidskip}{.5em}\sffamily\sloppy \synt{typed\ consta...
...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 are often used 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 the 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, each element of the record should be specified, 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 a compile-time error will occur.


3 Resource strings

A special kind of constant declaration part is the Resourestring part. This part is like a Const section, but it only allows to declare constant of type string. This part is only available in the Delphi or objfpc mode.

The following is an example of a resourcestring definition:

Resourcestring

  FileMenu = '&File...';
  EditMenu = '&Edit...';
All string constants defined in the resourcestring section are stored in special tables, allowing to manipulate the values of the strings at runtime with some special mechanisms.

Semantically, the strings are like constants; Values can not be assigned to them, except through the special mechanisms in the objpas unit. However, they can be used in assignments or expressions as normal constants. The main use of the resourcestring section is to provide an easy means of internationalization.

More on the subject of resourcestrings can be found in the Programmers' guide, and in the chapter on the objpas later in this manual.



Free Pascal Compiler
2001-09-22