Expressions
Expression in Coco is anything that has a value. It can be arithmetic expression, literal (e.g. a string) or return value of a function call.
Literals
Coco uses literals as fixed values in the source code. All literals from the source code are encoded in the manifest. Coco has the following literals:
Boolean values: true and false
Integer values: 0, 1, 123123, also hexadecimal values like 0xabcd
Bytes: a vector of bytes, written as hexadecimal value 0xabcdef
String: "foo", "bar"
Array: []U64{1, 2, 3}
Map: Map[U64]String{1: "yes", 2: "No"}
Object (class): Person{name: "Alice", age: 25}
Logic Expressions
Logic expressions use the following precedence of operators (from lowest to highest)
|| or
&& and
! not
Comparison operators also return a boolean value:
==
<
>
<=
>=
Arithmetic Operators
Arithmetic operators work with numeric types, i.e. U64, I64 and U256. The following operators are supported:
+
-
*
/
% (modulo)
Bitwise Operators
Bitwise operators are also supported on numeric types:
| bitwise or
& bitwise and
^ bitwise xor
Assignments
Coco supports regular assignments to variables, e.g.
memory x U64
x = 10
Assignments to multiple variables are supported, even from the functions that return multiple values
memory a, b U64
a, b = 10, 20
a, b = (x, y) <- xy()
function xy() -> (x, y U64):
x, y = 10, 20
Typecast
Typecast operators are also expressions that convert the value of one type into another.
Type(value), e.g. Bool(x), U64(10)