Constants

Constants represent pre-defined and unchangeable values associated with a type.

They are similar in concept to Fields, but their value is determined at compile time and, as the name implies, will remain unchanged during the the course of the execution of the program.

Constants are automatically considered static, and are available both on instances of a type as well as on the type itself (according to their visibility level, of course).

Constants are not to be confused with [Read-only Fields](Fields#read-only fields), which are initialized at runtime and can be different for each instance of a class and – most notably – can contain more complex values.

When a constant is used from code, its value is inlined. This is an important distinction when using constants defined from referenced libraries: if the value for a constant changes in a newer version of the library, but the code using the constant is not recompiled, it will continue to use the value of the constant that it was compiled with.

Declaration

Similar to Fields and the var keyword, constants are defined with the const keyword. After the const keyword, every subsequent item will be considered a constant.

To emphasize the constant and unchanging nature, constants use the = equals operator to specify their value, not the := assignment operator as used for Field or Property Initializers.

type
  MyClass = public class
  private
    const PI = 3.14;
    const E = 2.7;
    const Five: Double := 5
    const Hello = 'Hello';
    ...
    var AnyNumber := 5;
  end;

An optional type name can optionally be specified, to override the default type inferral. In the above example "5" would normally infer to be an Integer type, but here Five will explicitly become a Double, instead.

Visibility

The visibility of constants is governed by the Visibility Section of the containing type the method is declared in, or the Visibility Modifiers applied to the constants.

Static Constants

As already mentioned above, constants are always static, and do not need to (in fact, cannot) be prefixed with the class keyword nor can they have the static Modifier applied.

Virtuality and Other Modifiers

Polymorphism and other modifiers do not apply to constants.

See Also