Local Constants

Similar to Variables, the const statement is used to declare and optionally initialize a new constant in the current scope. The constant will be available for all code below its declaration, up until the end of the current scope – that is either to the end of the current method, if the variable is declared on that level, or to the end of the block Statement that includes the declaration.

Constant values need to be initialized with a value that can be determined at compile time, and – as the name implies – their value cannot change.

The initializer can be a simple literal, or simple expression combining one or more constant values (for example, the concatenation of two strings, or thje product of two integer constants

const PI = 3.14;
const NAME = FIRST + ' ' + LAST;
const AREA = 5.3 * 8.9;

Type Inference

The type of a constant is usually inferred from the (required) initializer, but an explcit type can be provided in order to make it explicit, or to overide the default inference:

const I = 5; // Int32
const U: UInt64 = 5;
const D: Double = 5;

Modifiers

No modifiers are allowed on const declarations.

See Also