Skip to main content

Expressions

An expression is anything that produces a value: arithmetic operations, literals, or function return values.

Literals

Fixed values in source code:

TypeExample
Booleantrue, false
Integer0, 123, 0xabcd
Bytes0xabcdef
String"hello", f"x={x}"
Array[]U64{1, 2, 3}
MapMap[U64]String{1: "yes", 2: "no"}
ClassPerson{name: "Alice", age: 25}

Operators

Logical Operators

OperatorDescriptionPrecedence
||ORLowest
&&ANDMedium
!NOTHighest
if x > 0 && y > 0:
// both positive

if !is_valid || is_expired:
throw "Invalid"

Comparison Operators

OperatorDescription
==Equal
!=Not equal
<Less than
>Greater than
<=Less than or equal
>=Greater than or equal

Arithmetic Operators

OperatorDescription
+Addition
-Subtraction
*Multiplication
/Division
%Modulo
memory result = (a + b) * c / 2
memory remainder = total % 10

Bitwise Operators

OperatorDescription
|Bitwise OR
&Bitwise AND
^Bitwise XOR
memory flags = flag1 | flag2
memory masked = value & 0xFF

Assignments

Single Assignment

memory x U64
x = 10

Multiple Assignment

memory a, b U64
a, b = 10, 20

From Function Returns

memory x, y = (a, b) <- GetPair()

function GetPair() -> (a U64, b U64):
a, b = 10, 20

F-Strings (Formatted Strings)

Interpolate expressions into strings using f"..." with {expression} placeholders:

memory name = "Alice"
memory greeting = f"Hello {name}" // "Hello Alice"
memory s = f"X={x}" // "X=123"
memory s2 = f"{x}={x}" // "123=123"

// Expressions in placeholders
throw f"mypkg value {x[2] * 50} != io2 value {y_arr[2] * 100 / 2}"
emit f"Transferring {amount} tokens"

Type Casting

Convert values between types using the type name as a function:

memory b = Bool(1)        // true
memory n = U64("123") // 123
memory s = String(42) // "42"
memory big = U256(100) // 100 as U256

See the type conversion table for supported conversions.

Methods on Primitive Types

Primitive types have built-in methods accessible via dot notation:

String Methods

MethodReturnsDescription
s.Get(idx: N)StringCharacter at index N (same as s[N])
s.ToBytes()BytesConvert string to bytes

Numeric Methods

MethodAvailable OnReturnsDescription
.Abs()I64I64Absolute value
.ToU64()Bool, Bytes, String, I64, U256U64Convert to U64
.ToI64()Bool, Bytes, String, U64, U256I64Convert to I64
.ToU256()Bool, Bytes, String, U64, I64U256Convert to U256
.ToBytes()String, Identifier, U64, I64, U256BytesConvert to Bytes
memory x I64 = -10
memory pos = x.Abs() // 10
memory n = some_i64.ToU64() // same as U64(some_i64)

Both method form (.ToU64()) and cast form (U64(value)) are valid and equivalent.

String Escape Sequences

EscapeResult
\"Literal double quote
\nNewline
\tTab
\\Literal backslash

Strings can span multiple source lines. Indentation in continuation lines is included in the string value:

memory s = "My spl
it string" // "My spl\nit string"

Increment and Decrement

x++    // increment by 1
x-- // decrement by 1

Compound Assignment

x += 5
x -= 3
x *= 2
x /= 4
x %= 10