Constants
A constant is a value fixed at compile time: the literal values
written in source and the named immutable bindings introduced by
const.
Literals
Literals are typed by their lexical form (Lexical elements). The typing rule is:
- An integer literal is an integer constant.
- The reserved words
trueandfalseare boolean constants. - Single-quoted or double-quoted strings are string constants.
- Sequences of word characters not matching the above (called barewords) are string constants.
42 # integer constant
-7 # integer constant (the sign is part of the literal)
true false # boolean constants
say # string constant "say"
'42' # string constant "42" — single-quoting forces string type
"42" # string constant "42" — double-quoting does too
'true' # string constant "true" — not the boolean
An empty string literal — '' or the empty "" — is the empty string, a
string value. It is not null; null arises only from the empty contexts
listed in Types.
Named constants — const
const binds a name to a value that cannot subsequently be mutated by
set. It exists in two forms.
Top-level const
At the top level, const name value introduces an immutable binding in
the variable namespace, read as $name
(Declarations and scope). The value may be
of any type — integer, string, list, block, etc. Top-level names are
hoisted (Declarations and scope).
const starting_gold 100
const greeting 'well met, traveler'
const predicates ({ <n> [gt $n 0] } { <n> [lt $n 10] })
Nested const
Inside a block body, const name value creates an immutable local
binding, evaluated in place with no hoisting. It behaves like let
(Variables) except that the binding refuses later
mutation by set. A nested const that names something already bound in
the same scope is a compile-time error
(Declarations and scope).
Immutability
const controls only whether set may rewrite the binding. It does not
make the referenced value deeply immutable: a const bound to a list or
an entity reference still refers to a mutable game object, and persistent
entity slots written with store are unaffected by const
(Variables).