Constants are a way of giving a name to shared, static values inside of a
The constant's must be known at compilation. The constant's value is stored in the compiled module or script. And each time the constant is used, a new copy of that value is made.
Constant declarations begin with the
const keyword, followed by a name, a type, and a value. They can exist in either a script or module
Constants must start with a capital letter
Z. After the first letter, constant names can contain underscores
Z, or digits
Even though you can use letters
z in a constant. The general style guidelines are to use just uppercase letters
Z, with underscores
_ between each word.
This naming restriction of starting with
Z is in place to give room for future language features. It may or may not be removed later.
public constants are not currently supported.
const values can be used only in the declaring module.
Currently, constants are limited to the primitive types
vector<u8>. Future support for other
vector values (besides the "string"-style literals) will come later.
consts are assigned a simple value, or literal, of their type.
In addition to literals, constants can include more complex expressions, as long as the compiler is able to reduce the expression to a value at compile time.
Currently, equality operations, all boolean operations, all bitwise operations, and all arithmetic operations can be used.
If the operation would result in a runtime exception, the compiler will give an error that it is unable to generate the constant's value
Note that constants cannot currently refer to other constants. This feature, along with support for other expressions, will be added in the future.