Skip to main content

Syntax Summary

A one-page cheat sheet. Each construct links to the chapter that covers it.

Literals & basics — Basics

# comment (line comments only)
42, 3.14, 1_000, 0xCAFE # Int64, Float64, separators, hex
"text\n", `no \escapes` # strings; backticks are literal
true, false, nil
[1, "two", []] # list, 1-indexed: xs[1]
even?(x), put!(chan, x) # ? predicates, ! side effects

Statements end at newlines or commas. {a, b}, [a, b], (a, b) are the same to the parser. Precedence: ^ * / + -, then comparisons (non-chaining), then &&, ||; parenthesize anything unusual.

Variables — Variables & Scope

x = 1 # create or update; no declarations
let x = 2 { ... } # shadow within the block

Assignment updates an existing variable in the function, else creates one in the nearest { }. Functions can't assign to outer variables.

Functions — Functions

fn square(x) { x * x } # implicit return
fn pow(x, n: Int) { ... } # optional type annotation
fn seq(x, xs...) { ... } # splat (also spreads: f(xs...))
fn inc(&x) { x = x + 1 } # swap parameter…
inc(&x) # …and call: sugar for x = inc(x)

No lambdas or keyword arguments yet.

Data — Values & Data

bundle Tape(data: List, i: Int64) # fields
bundle Maybe { Some(x), Nil() } # variants
fn (a: T) + (b: T) { ... } # operator method
fn (xs: T)[i] { ... } # indexing method

Everything is a value: variables never share mutable state.

Patterns — Pattern Matching

[a, _] = xs # destructure; _ ignores
Some(x) = m # constructor pattern (aborts on mismatch)
fn f($Answer) { ... } # $v matches the value in v
fn f(x: (Int | UInt)) { ... } # type union
if let Some(x) = m { } else { }
match m {
let Some(x) { ... }
let Nil() { ... }
}

Dispatch — Multiple Dispatch

fn fib(n) { fib(n-1) + fib(n-2) }
fn fib(1) { 1 } # value patterns; latest match wins

@extend # add a method to another module's fn
fn show(Celsius(deg)) { ... }

Control flow — Control Flow

if a { } else if b { } else { } # an expression
while cond { break, continue }
for x = xs { ... } # iterables; value is nil
a && b, a || b # short-circuit

@label outer
for ... { break outer } # labelled loops & blocks

Modules — Modules & Exports

export { foo, Bar }
export { ... } from "./impl.rv" # re-export
import { map } from "common"
import { x } from "./file.rv"
import { ... } from "./file.rv" # wildcard

Macros & attributes — Macros & Attributes

show 2+2 # (2 + 2) = 4
test x == 4 # pass:/fail:
@doc """...""", @label, @extend
r`\d+` # regex template
js`return 2+2` # inline JS template (interpolate with \x)

JS interop — Calling JavaScript

js.Math.sqrt(5) # globalThis access
Float64(js.Math.sqrt(5)) # convert back
new(js.TextEncoder)
await(promise) # plain function, no coloring
async { slow() } # spawn a task