Expressions
An expression is anything that produces a value: arithmetic operations, literals, or function return values.
Literals
Fixed values in source code:
| Type | Example |
|---|---|
| Boolean | true, false |
| Integer | 0, 123, 0xabcd |
| Bytes | 0xabcdef |
| String | "hello", f"x={x}" |
| Array | []U64{1, 2, 3} |
| Map | Map[U64]String{1: "yes", 2: "no"} |
| Class | Person{name: "Alice", age: 25} |
Operators
Logical Operators
| Operator | Description | Precedence |
|---|---|---|
|| | OR | Lowest |
&& | AND | Medium |
! | NOT | Highest |
if x > 0 && y > 0:
// both positive
if !is_valid || is_expired:
throw "Invalid"
Comparison Operators
| Operator | Description |
|---|---|
== | Equal |
!= | Not equal |
< | Less than |
> | Greater than |
<= | Less than or equal |
>= | Greater than or equal |
Arithmetic Operators
| Operator | Description |
|---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulo |
memory result = (a + b) * c / 2
memory remainder = total % 10
Bitwise Operators
| Operator | Description |
|---|---|
| | 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
| Method | Returns | Description |
|---|---|---|
s.Get(idx: N) | String | Character at index N (same as s[N]) |
s.ToBytes() | Bytes | Convert string to bytes |
Numeric Methods
| Method | Available On | Returns | Description |
|---|---|---|---|
.Abs() | I64 | I64 | Absolute value |
.ToU64() | Bool, Bytes, String, I64, U256 | U64 | Convert to U64 |
.ToI64() | Bool, Bytes, String, U64, U256 | I64 | Convert to I64 |
.ToU256() | Bool, Bytes, String, U64, I64 | U256 | Convert to U256 |
.ToBytes() | String, Identifier, U64, I64, U256 | Bytes | Convert 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
| Escape | Result |
|---|---|
\" | Literal double quote |
\n | Newline |
\t | Tab |
\\ | 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