Operators
This content is for v0.7. Switch to the latest version for up-to-date documentation.
Arithmetic operators
Section titled “Arithmetic operators”| Operator | Description | Example |
|---|---|---|
+ | Addition / string concatenation | 10 + 3 → 13 |
- | Subtraction | 10 - 3 → 7 |
* | Multiplication | 10 * 3 → 30 |
/ | Division | 10 / 3 → 3.333... |
% | Modulo (remainder) | 10 % 3 → 1 |
- (unary) | Negation | -10 |
+ (unary) | Identity | +10 |
Decimal arithmetic works as expected:
5.5 / 2.0; // 2.75The + operator also concatenates strings:
"Hello" + ", " + "World!"; // "Hello, World!"Cross-type behavior
Section titled “Cross-type behavior”+concatenates two Strings, adds two Numbers, or transposes an Event by a Number (semitones)-subtracts two Numbers, or transposes an Event down by a Number*,/,%work on Numbers only
// Event transposition (inside pattern callbacks / schedulers)event + 7; // transpose up a fifthevent - 12; // transpose down an octave
// Direct note arithmetic is a type errorC4 + 2; // type error — use pattern combinators for transpositionNUL behavior
Section titled “NUL behavior”NUL does not propagate through arithmetic — using NUL as an operand is a type error.
NUL + 1; // type error5 * NUL; // type errorComparison operators
Section titled “Comparison operators”All comparison operators return a boolean (true or false).
| Operator | Description | Example |
|---|---|---|
== | Equal | 5 == 5 → true |
!= | Not equal | 5 != 3 → true |
< | Less than | 3 < 5 → true |
> | Greater than | 5 > 3 → true |
<= | Less than or equal | 5 <= 5 → true |
>= | Greater than or equal | 5 >= 3 → true |
String comparison is also supported:
"apple" == "apple"; // trueCross-type behavior
Section titled “Cross-type behavior”Equality (==, !=) works across types in two cases:
- Note ↔ Number — compared by MIDI value
- NUL ↔ anything —
NUL == NUListrue; NUL equals nothing else
C4 == 60; // true — Note compared by MIDI valueD4 != 62; // falseNUL == NUL; // trueNUL == 0; // false — NUL is not zeroNUL == false; // false — NUL is not falseOrdering operators (<, >, <=, >=) compare Number, Note, and Event values — all coerce to their numeric (f64) representation:
D4 > C4; // true — 62 > 60A4 >= 69; // trueNUL behavior
Section titled “NUL behavior”NUL can be tested for equality but not ordered:
NUL == NUL; // true5 != NUL; // trueNUL < 1; // type error — NUL has no orderingLogical operators
Section titled “Logical operators”| Operator | Description | Example |
|---|---|---|
&& | Logical AND (short-circuit) | true && false → false |
|| | Logical OR (short-circuit) | true || false → true |
! | Logical NOT | !true → false |
Short-circuit evaluation means && stops at the first false and || stops at the first true:
let val = 7;val > 5 && val < 10; // trueLogical operators require Boolean operands — there is no truthy coercion. See Truthiness for where truthiness applies.
true && 0; // type error — 0 is a Number, not a Boolean!NUL; // type error — NUL is not a BooleanCompound assignment operators
Section titled “Compound assignment operators”These combine an arithmetic operation with assignment. They follow the same type rules as their arithmetic counterparts.
| Operator | Equivalent |
|---|---|
+= | x = x + value |
-= | x = x - value |
*= | x = x * value |
/= | x = x / value |
%= | x = x % value |
let count = 1;count += 1; // 2count *= 3; // 6Output operator <<
Section titled “Output operator <<”The << operator sends a pattern or value to a target — a track or an effect parameter.
melody << [C4 E4 G4 E4]; // send pattern to a MIDI trackecho.Feedback << 0.6; // set an effect parameterfilter.Cutoff << Sine(2).range_exp(400, 4000); // modulate with a signalFor full details see MIDI Tracks, Effects, Routing, and Signals & Automation.
Routing operator >>
Section titled “Routing operator >>”The >> operator routes audio from one node to another. Without explicit routing, tracks go to master automatically.
kicks >> drum_bus;snares >> drum_bus;drum_bus >> master;See Routing for submix buses and routing chains.
Reference operator
Section titled “Reference operator”The & operator creates a reference to an array, dictionary, or class instance, enabling shared mutable state. It only works on identifiers — not arbitrary expressions — and only on compound types (using & on a primitive like a Number or String is a type error).
References auto-dereference transparently: field access, method calls, and operators work the same whether you hold a value or a reference.
let nums = #[1, 2, 3];let nums_ref = &nums;
nums_ref.push(4);PRINT nums; // [1, 2, 3, 4] — both see the changeSee References for details.
Operator precedence
Section titled “Operator precedence”From highest to lowest:
- Unary (
-,+,!) - Multiplicative (
*,/,%) - Additive (
+,-) - Comparison (
<,>,<=,>=) - Equality (
==,!=) - Logical AND (
&&) - Logical OR (
||)
<< and >> are statement-level operators (not part of expression precedence). They bind an entire right-hand expression to a target.
Parentheses override precedence:
2 + 3 * 4; // 14 (multiplication first)(2 + 3) * 4; // 20 (parentheses first)Type compatibility
Section titled “Type compatibility”Quick reference for which types work with which operators.
| Operator | Number ↔ Number | String ↔ String | Note ↔ Number | Event ↔ Number | NUL as operand |
|---|---|---|---|---|---|
+ | Number | String | error | Event | error |
- | Number | error | error | Event | error |
* / % | Number | error | error | error | error |
== != | Boolean | Boolean | Boolean | error | Boolean |
< > <= >= | Boolean | error | Boolean | Boolean | error |
&& || ! | error | error | error | error | error |
- Logical operators accept only Boolean ↔ Boolean.
&accepts only identifiers bound to Array, Dict, or class Instance.<<accepts Track/Parameter on the left; Pattern/Signal/Value on the right.>>accepts AudioTrack on both sides (ormasteron the right).