Constants
Constants are declared with const
. They can only be defined
at the module level (outside of functions).
Constant values can never be changed. You can also declare a single
constant separately:
V constants are more flexible than in most languages. You can assign more complex values:
* WIP - for now function calls are evaluated at program start-up
Global variables are not normally allowed, so this can be really useful.
Modules
Constants can be made public with pub const
:
The pub
keyword is only allowed before the const
keyword and cannot be used inside
a const ( )
block.
Outside from module main all constants need to be prefixed with the module name.
Required module prefix
When naming constants, snake_case
must be used. In order to distinguish consts
from local variables, the full path to consts must be specified. For example,
to access the PI const, full math.pi
name must be used both outside the math
module, and inside it. That restriction is relaxed only for the main
module
(the one containing your fn main()
), where you can use the unqualified name of
constants defined there, i.e. numbers
, rather than main.numbers
.
vfmt takes care of this rule, so you can type println(pi)
inside the math
module,
and vfmt will automatically update it to println(math.pi)
.