Syntax reference
This page collects Pluvial’s surface syntax. For explanations, follow the links into the language guide.
Comments
Section titled “Comments”# line comment to end of lineauto x = 1 # trailing comment
## doc comment — attaches to the declaration below itdef f() {}Literals
Section titled “Literals”42 # int3.14 # floattrue false # bool"text" # stringnull # absence (for nullable types)[1, 2, 3] # array literal{"a": 1, "b": 2} # map literal(1, "hello") # tuple literalf"hi {name}" # f-string interpolationString escapes: \n, \t, \\, \", \r, \0, plus \xNN and \uXXXX. Any other
escape is a compile error.
Variables
Section titled “Variables”int age = 25float rate = 1.5bool active = truestring name = "Pluvial"int? maybe = null # nullableauto count = 10 # inferred, then fixedThe initializer is required; with an explicit type it must match exactly. See Variables.
| Form | Meaning |
|---|---|
int float bool string | base types |
auto | inferred |
T? | nullable |
array<T> | array |
map<K, V> | map |
(T1, T2) | tuple |
Result<T> | result with a string error |
Result<T, E> | result with a struct error E |
(T1, T2) => R | function type |
| a struct / enum name | user-defined type |
See Types.
Operators
Section titled “Operators”Arithmetic
Section titled “Arithmetic”+ - * / % ** — same numeric type on both sides; no implicit promotion. string + string
concatenates. / truncates for int. ** is right-associative.
Bitwise (int only)
Section titled “Bitwise (int only)”& | ^ ~ << >> — int operands only; shifts are arithmetic.
Comparison / equality
Section titled “Comparison / equality”< > <= >= (same numeric type), == != (same type). string == string compares contents.
x == null / x != null only for nullable x.
Logical
Section titled “Logical”&& || ! — bool only, short-circuiting.
is—r is ok/r is err→bool.??— null coalescing (T? ?? T → T)..— member / method access;?.— optional chaining on a nullable receiver.[i]— index access.
Precedence (low → high)
Section titled “Precedence (low → high)”|| → ?? → && → | → ^ → & → == != is →< > <= >= → << >> → + - → * / % → ** (right-assoc) →unary ! - ~ → . / [i] → grouping ( )Augmented assignment
Section titled “Augmented assignment”+= -= *= /= %= **= &= |= ^= <<= >>= — sugar for x = x op e. Left-hand side may be a
variable, an array element, or a struct field. See
Variables.
Control flow
Section titled “Control flow”if (cond) { ... } else if (cond2) { ... } else { ... }
while (cond) { ... }
for item in arr { ... }for (i, x) in arr { ... } # enumeratefor k in m { ... }for (k, v) in m { ... }
break continueouter: while (cond) { break outer continue outer } # labeledConditions must be bool; braces are mandatory. See
Control flow.
match expr { 1 | 2 => { ... } # literal / OR ok(v) => { ... } # Result ok err(e) => { ... } # Result err null => { ... } # nullable absent binding => { ... } # nullable present (int x, string s) => { ... } # tuple Shape.Circle(r) => { ... } # enum variant _ => { ... } # wildcard, last}Match is a statement; arms use braces and are checked for exhaustiveness.
Functions
Section titled “Functions”def add(int a, int b) int { return a + b }def greet(string name) { println(name) } # no return valuedef connect(string host, int port = 8080) { ... } # default argument
add(1, 2)greet(name: "x") # named argumentLambdas and function types:
auto f = (int x) => x * 2auto g = (int x) => { return x + 1 }(int) => int h = fSee Functions.
Structs and enums
Section titled “Structs and enums”struct Point { int x int y def translate(int dx, int dy) Point { return Point { x: self.x + dx, y: self.y + dy } }}
Point p = Point { x: 1, y: 2 }Point p2 = Point { ...p, x: 5 } # struct update
enum Direction { North South East West }enum Status { Ok = 200 NotFound = 404 }enum Shape { Circle(float r) Rectangle(float w, float h) Point }See Structs.
Modules
Section titled “Modules”export def add(int a, int b) int { return a + b }export struct Point { int x int y }
import "math" # math.add(1, 2)import "./utils" as u # u.namefrom "math" import add, mul as timesimport std.io # std/ shorthandfrom std.http import getSee Modules.
Error handling
Section titled “Error handling”Result<int> r = to_int("42")if (r is ok) { println(r.value) } else { println(r.error) }
match r { ok(v) => { println(v) } err(e) => { println(e) }}See Error handling.