Constants & Variables
The next building block after types are constants and variables.
Constants
Constants are immutable by design, which means their values cannot be
reassigned. Put in another way, they can be assigned once, at their
declaration. When defining a constant you need to provide a name
,
type
and a value
:
Constants in JsLIGO are enforced:
Unlike the other syntaxes, JsLIGO doesn't allow variable names to be reused in the same block scope:
However, the following does work:
You can evaluate the constant definition above using the following CLI command:
Variables
Variables, unlike constants, are mutable.
⚠️ Please be wary that mutation only works within the function scope itself, values outside of the function scope will not be affected. In other words, when a function is called, its arguments are copied, as well as the environment. Any side-effect to that environment is therefore lost when the function returns.
You can run the add
function defined above using the LIGO compiler
like this:
Escaped Identifiers
Both variables and constants are, at the level of the lexicon,
identifiers. Each flavour of LIGO has its own set of
keywords. Sometimes we need an identifier that is the same as a
keyword, or, perhaps, we do not want to shadow a predefined
identifier, like amount
. In those cases, you could suffix your
identifier with an underscore, like amount_
. (Beware that if you
prefix with an underscore, like _amount
, the compiler will not
complain about the value being not used.) But this is not a good
practice because we do not pronounce aloud the underscores, and there
is the issue of one or two underscores. To solve all those problems,
in LIGO, you can prefix you identifier with @
, like @amount
.