Constants
A constant is a location where a value is stored, and its value cannot change during the runtime of the program.
To create a constant, you have to specify its identifier and type, known as declaring a constant, and provide a value for the constant, known as initializing a constant.
A constant must be initialized when it is declared, and must be declared before it can be used.
Constant creation has the following syntax:
Identifier : type = expression

Constants declared in a function can omit the type:
Identifier := expression

If the type is omitted, the constant's type is inferred from the expression used to initialize the constant. Only local constants can omit the type because the type describes how the constant can be used. A constant in a module makes up part of the interface of the module that contains it. Without the type, that interface is nonobvious.
In the following example, a random number is generated in each iteration of the loop and used to initialize the constant RandomNumber
. The random number will only break out of the loop it is less than twenty.
loop:
Limit := 20
# For local constants, the type can be omitted.
RandomNumber : int = GetRandomNumber()
# Providing the type explicitly can make the code easier to read.
if (RandomNumber < Limit):
break
Note that in each loop iteration, a new constant named RandomNumber
is introduced and assigned the result of GetRandomNumber()
as its value.
Variables
In addition to the constants described above, Verse also has variables.
Variables are similar to constants, but are defined with the keyword var
, which means you can change their values at any point.
For example,
var MaxHealthUpgrade : int = 10
is an integer variable, and its value may not always be 10
.
Variable creation has the following syntax:
var Identifier : type = expression

Note that the type must be explicitly specified for variables.
After you create a variable, you can assign a different value to it with the following syntax:
set Identifier = expression

Aside from =
, a variety of other operators can be used to mutate a variable. For example,
var X:int = 0
set X += 1
set X *= 2