Using the memory
Keyword
In Coco, the memory
keyword is your key to declaring variables effortlessly. Just provide the variable names followed by their respective types, as shown in Variable.DeclareVar()
Using the storage
Keyword
A special case of a variable is a pointer to a stored value, in this case we need to declare such variable with storage
keyword. While for primitive values using storage
is possible, though not necessary, for complex values like arrays, maps or classes, using storage
can be more efficient that dispersing large data structures from the storage into memory.
Direct Value Assignment
Experience the convenience of direct value assignment! Coco automatically determines the variable's type based on the assigned value, as shown in Variable.DeclareValue()
Defining Constants Using const
Keyword
Constants can be defined for the whole module using const
keyword. Constant’s definition needs to include both type and value.
Retrieval of variable type with typeof
A string description of variable type can be retrieved using typeof(variable)
function. Any variable or expression can be the argument, except functions. Calling functions in typeof()
produces a compiler error.
coco Variable
const VAL U64 = 10
endpoint invokable DeclareVar() -> (output U64):
memory n U64
n = VAL
yield output n
endpoint invokable DeclareValue() -> (output U64):
memory n = 10
yield output n
coco GuardianRegistry
state persistent:
Operators Map[String]Operator
class Guardian:
field KramaID String
field OperatorID String
class Operator:
field Identifier String
field Guardians []String
endpoint deployer Init():
pass
endpoint invokable persistent RegisterGuardian(guardian Guardian):
storage operator Operator
memory operatorID = String(Address(Sender))
observe operators <- GuardianRegistry.State.Operators:
operator = operators[operatorID]
mutate operators <- GuardianRegistry.State.Operators:
append(operator.Guardians, guardian.KramaID)
operators[operatorID] = operator