- Use
valfor immutable variables andvarfor mutable variables. - Use
constorglobaloutside functions.
Declaration
-
valdeclares a variable that is assigned exactly once: -
vardeclares a variable that can be reassigned:
Explicit types
A variable may declare its type explicitly. If the type is not specified, it is inferred from the initial assignment:Unassigned variables
A variable may be declared without an initial value, but it must be assigned before its first use:Multiple variables
Declaring multiple variables at once is tensor destructuring:Nested scope
A block{ ... } introduces a nested scope:
Function parameters
Function parameters behave the same as local variables. They can be reassigned, but the reassignment does not affect the caller’s state:userId visible in demo, declare the parameter with mutate: mutate userId.
Constants
Global-scope constants are declared withconst outside functions:
stringCrc32("...") and similar.
To group integer constants, use enums.
Global variables
Use theglobal keyword to declare variables outside functions:
global must have an explicit type and cannot be initialized at the point of declaration. Initialization is done manually at some point in a program. A contract has several entry points, such as get fun, onInternalMessage, and others.
A global must be initialized along the execution path where it is required.
lazy loading.