Expression reference
Use expressions in ability attributes, function bodies and string interpolation. For declarations and types, see Types and expressions.
On this page
Operators and literals#
+ - * / % (after a number, - is minus: 5-3 is 2; a name keeps its hyphens, so write n - 1 to subtract from a name), comparisons, &&, ||, !, parentheses. Strings compare by content with == and !=; a one-character literal compared with a Char is a Char. A Pointer or Bytes value cannot be ordered with <, >, <= or >=; compare pointers with == and !=.
Numbers may be written in hexadecimal (0xF0) or binary (0b1111_0000), with _ between digits; fmt keeps the spelling.
Names and references#
| Form | Reads |
|---|---|
var.x, const.X, param.x | A variable, constant or command-line parameter |
data.KIND.NAME.ATTRIBUTE | An attribute exposed by a data block |
module.calc.total | An output of a module instance |
Enum.value | A member of a declared enum |
a.b.c, data.value.guests.current[i].name | A field or an indexed element and its field |
Inside a function, use the bare names of its parameters and locals.
Functions and conversions#
| Form | Purpose |
|---|---|
length(x) | Read the length of a collection or buffer |
int(x), float(x), uint8(x), int16(x), size(x), float64(x) … | Convert a number to the indicated type; the exact kinds never mix without a conversion, and a literal must fit the kind it fills (Types and expressions) |
min(a, b), max(a, b) | Select the smaller or larger number |
address_of(x), value_at(p) | Take an address or read through a pointer |
You can also call a function ability that declares a return type.
Bit operations#
bit_and(a, b), bit_or(a, b), bit_xor(a, b) and bit_not(a) work on whole numbers of one kind (a literal takes the other side's kind); shift_left(a, n) and shift_right(a, n) move the bits of a by n places, and shift_right copies the sign bit into a signed number. An amount that is negative or at least the width shifts every bit out: the result is 0, or -1 for shift_right of a negative number; a literal amount like that is an error.
Byte order#
uint16_big(b, at) reads a UInt16 from the Bytes value b at position at, highest byte first; uint16_little reads it lowest byte first. The same pairs exist for int16, int32, int64, uint32 and uint64, and the result has the kind in the name (int64 gives an Int). Reading past the length, or at a negative position, stops the program with a message that starts with the file, line and column of the expression. ability "put_number" (StructOp/c and StructOp/rust 2.1.0) writes a number: target (a Bytes value), at, value and order = "big" or "little". The value's kind sets how many bytes are written (UInt16 2, Int 8), so a bare number or a Size is an error: write uint16(512). Writing may overwrite bytes and may extend the length at the end, never leave a gap.
Native C expressions#
Use c"..." to embed a native C expression in a C-specific project. See the native expression example and the portability guide.