Syntax
Comments
Nolang supports single-line comment markers and a multi-line (block) comment marker:
;— single-line marker (comments to end-of-line);; <content>— single-line marker (when;;is followed by content on the same line, comments to end-of-line; same semantics as;);;\n— multi-line (block) comment: when;;is immediately followed by a newline (only whitespace allowed in between), it enters multi-line mode until another;;followed by a newline/EOF is encountered
; this is a comment
; this is also a comment, same semantics
;; this is still a single-line comment (no newline after ;;)
x = 1; trailing comment, runs to end of line
x = 2;; inline single-line comment, same semantics
;;
this is a multi-line (block) comment
it can span multiple lines
until a standalone ;; is encountered
;;
y = 3
;;
the closing ;; must be followed by a newline or EOF
to be recognized as the ending delimiter
;;
Multi-line trigger rule:
;;must be followed by only whitespace (spaces/tabs) up to a newline or EOF to enter multi-line mode. If;;is followed by any non-whitespace character on the same line, it is treated as a single-line comment (to end-of-line).Multi-line closing rule: The closing
;;must likewise be followed by a newline or EOF (only whitespace allowed in between). An unterminated multi-line comment runs to the end of the file.
Rule: One statement per line — single-line multi-statement writing is forbidden. The semicolon
;is a comment marker, never a statement separator, and,cannot join statements either. Every statement inside a block{ ... }must occupy its own line. This rule also applies to code examples inside comments: even there, never put multiple statements on one line with;or,.
#{fmt-skip}
; ❌ Wrong: `;` used as a statement separator — everything after `;` is a comment,
; including the closing `}`. The block never closes and the rest is silently dropped.
; cond -> { print('X'); return }
; ❌ Wrong: comma joining statements
; h0 = 1732584193, h1 = 4023233417
; out = from-i64(v), out = from-u64(v)
; debug(msg), info(msg), warn(msg)
; ✅ Correct: one statement per line
cond -> {
print('X')
return
}
; ✅ Correct: one statement per line
; h0 = 1732584193
; h1 = 4023233417
; out = from-i64(v)
; out = from-u64(v)
; debug(msg)
; info(msg)
; warn(msg)
Why
;is especially dangerous: incond -> { print('X'); return }the tail; return }is all comment, so the real code is onlycond -> { print('X'). The block never closes, and every statement after the stranded}(usually the whole remainder of the function body) is swallowed; diagnostics may also point far away from the actual mistake.
Data Types
Basic types
- byte
- bool ; lowercase only
- char ; character type: a single Unicode scalar value (rune), stored as i32. Wrapped in double quotes, e.g. "中"
- str ; string type, wrapped in single quotes 'hello', or backtick raw strings
multi-line - txt ; fixed-length text type, 256 bytes underlying (max 255 bytes of data), requires an explicit type annotation, e.g. t txt = 'abc'
- i8
- i16
- i32
- i64 ; default numeric type, architecture-independent
- i128 ; 128-bit signed integer
- u8
- u16
- u32
- u64
- u128 ; 128-bit unsigned integer
- usize ; FFI only
- f32
- f64
Container types
-
obj ; object
-
map ; map
-
arr ; fixed-length array
-
vec ; variable-length array
-
slice ; slice (view); has no independent data structure and must be backed by an arr/vec
-
* ; pointer; FFI
#{c}declarations and standard library only -
any ; any type; standard library only
Advanced types
- bigint
- err
The char Type (Unicode Code Point)
char represents a single Unicode scalar value. Its underlying storage type is i32, not i64:
- Valid range:
0 ..= 0x10FFFF(that is,0 ..= 1114111). The largest code point needs only 21 bits, soi32holds it comfortably. - Out of range is an error: when a literal falls outside that range, the compiler rejects it outright at compile time (including under
no vet). For examplec char = 0x110000✗ andc char = 1114112✗, whilec char = 1114111✓. - Arithmetic is allowed: a
charcan participate in integer arithmetic directly.z = a + 25andu = ch - 32(wherechis the loop variable offor ch <- s) are both perfectly normal and operate on the code point value. Arithmetic always happens at i32 width and the result is treated as an ordinary integer value; precisely because of this,+ - * /on achardoes not default tooption<int>the way the integer family does, so it never triggers an "unhandled integer overflow" compile error. The only thing to watch is range: the largest code point is0x10FFFF, so when a value is meant to go beyond i32/code-point semantics (an offset, a running counter), convert toi32/i64explicitly first — it makes the intent clearer. - Relationship to
str:s[i]returns achar, and acharconverts implicitly tostr(UTF-8 encoded). See Strings — Implicit Conversion.
"Underlying type" and "call-site conversion" are two different things. A
charisi32. The builtinstr_from_cp(cp char) -> ?strtakescharin its source signature; only when calling into the external runtime does the compiler zero-extend that i32 argument (zext i32 → i64) to match the IR parameter of@str_from_cp— a temporary conversion during argument preparation that does not change the type ofchar. It is like C'schar c='a'; f((long)c);:citself is one byte and is only promoted for the call.
Type Aliases and Union Types
A type alias creates a new name for an existing type. It uses the equals syntax name = type, supporting both single-type aliases and multi-type unions.
Syntax
; Union type: multiple types separated by |
int = i8 | i16 | i32 | i64 | i128 | u8 | u16 | u32 | u64 | u128
float = f32 | f64
num = int | float
string = str | txt
; Single type alias
bytes = []byte
buf = [16]u8
int,float,numare defined instd/number, andstringis defined instd/str— none of them are compiler built-in keywords; they are union types written by the standard library using this same alias syntax.
Chained References of Union Types
Union types can reference other union types to form a hierarchy:
#{fmt-skip}
int = i8 | i16 | i32 | i64 | i128 | u8 | u16 | u32 | u64 | u128
float = f32 | f64
num = int | float ; num is a union of int and float
string = str | txt ; string directly unions the two concrete string types
Using in Functions
Union types can be used for function parameters and return values. The compiler automatically performs monomorphization, generating a separate function version for each member type:
; Parameter type is the num union
max = (a ..num) (r num) {
r = a[0]
n = len(a)
i <- [1..n): {
a[i] > r -> r = a[i]
}
}
; Method defined on the union type
num.sign = () (r num) {
{
. > 0 -> r = 1
. < 0 -> r = -1
-> r = 0
}
}
string (= str | txt) works the same way for parameters and return values:
#{fmt-skip}
; Both str and txt arguments are accepted; each is monomorphized into a concrete function
take = (s string) (n i64) {
n = len(s)
}
a str = 'hello'
b txt = 'hi'
n1 = take(a) ; take__str
n2 = take(b) ; take__txt
Known limitations of union-typed function bodies (identical for
numandstring): calling a member method on a union parameter inside the body (e.g.s.len()) is not yet supported — use a global builtin (len(s), byte count) or split into concrete-typed functions; declaring a union-typed variable (s string = 'x') is also not yet supported. In addition, never define an alias method that shares a member method's name (e.g.string.len) — it takes overs.len()calls and re-dispatches to itself inside its own body, causing infinite recursion.
Detection Rules
The equals syntax is recognized as a type alias (rather than a variable assignment) in the following cases:
name = type | type | ...: union type (contains|)name = []type: slice typename = [N]type: array typename = ?type: optional typename = known-type: single type alias, whereknown-typeis a built-in type name (such asi64,f64,bool,str, etc.) or a previously defined type alias name
Variable Declaration
#{fmt-skip}
; Variables have no keyword
; i64, f64, byte, bool, byte, str can omit the type annotation
i = 1
; f64 has a . in the middle
f = 1.0
; byte
b = 0x00
; i8 — if the variable name matches the type name, the type annotation can be omitted
i8 = 3
; An explicit annotation equal to the inferred type is redundant (no vet reports tcpoxtfd; no fmt --fix=redundant removes it automatically)
x str = ''
x = '' ; equivalent, the annotation is redundant
; Default zero value
; Variable definitions do not need to be declared in advance
u16
; str wrapped in single quotes
name = 'nolang'
; bool true/false all lowercase
flag = true
flag = false
; bool shorthand: !! equals true, ! equals false (standalone literals)
flag = !! ; true
flag = ! ; false
; Variable assignment
; Same names are not allowed; if a name already exists, it is treated as modifying the variable
name = 'hello'
name = 'world'
; String concatenation
greeting = 'hello, ' - name
; Raw string (backtick-wrapped, multi-line, no escape processing)
sql = `
SELECT id,name
FROM user
WHERE id > 100
`
; Explicit type annotation
a u64 = 10
; Hex literal type inference:
; - Decimal integer literals (e.g. 771) infer to i64 (default integer type)
; - Hex literals (e.g. 0x0303) infer to byte (u8)
; - If a hex value exceeds the byte range (> 255), you MUST add an explicit
; type annotation to avoid incorrect truncation:
PORT i64 = 0x0303 ; ok: explicit i64, value = 771
; PORT = 0x0303 ; WRONG: inferred as byte, value truncated!
; PORT = 771 ; ok: decimal defaults to i64
; - Hex literals should use lowercase letters (0x00ff, not 0x00FF)
; - Recommendation: use decimal for general integer constants; use hex with
; explicit i64 type annotation only for protocol/bitmask constants.
; Character (double quotes = char/rune, a single character)
c = "中"
; byte type
b = 0x00
; arr fixed-length array
arr [3] = [1, 2, 3]
; vec dynamic array (slice)
vec = [4, 5, 6]
; Explicit type (slice)
typed []u8 = [1, 2, 3]
; Array
typed [3]u16 = [1, 2, 3]
; Length automatically inferred (i64)
a [?] = [1, 2, 3]
Raw String
A raw string is declared with a pair of backticks (`) and has type str.
Syntax
sql = `
SELECT id,name
FROM user
WHERE id > 100
`
Mandatory Formatting Rules
- The opening
`must be immediately followed by a source newline (LF; CRLF in Windows files is accepted too); - The closing
`must sit on a line of its own, and that line may contain nothing but whitespace plus the closing backtick; - The two backtick lines are not part of the string content — they serve only as delimiters;
- Escaping: every
\,\n,\t,\',\"inside is kept verbatim, with no escape processing of any kind; - Source newlines and indentation are preserved exactly in the string's bytes — except CR (
\r), which is always dropped, so a CRLF file yields byte-for-byte the same value as an LF file; - A backtick character cannot be embedded directly. If you need one, concatenate an ordinary single-quoted string instead.
Example
; The content is "SELECT id,name\nFROM user\nWHERE id > 100\n"
; Escape sequences \n \t are kept verbatim, not interpreted
raw = `
line1\nline2
\ttabbed
`
Regex Literals
Nolang supports JavaScript-style regex literals /pattern/flags, which create a compiled regexp instance.
Syntax
; Basic regex literal
re = /\d+/
; With flags
re = /hello/gi
; Character classes, anchors, quantifiers
re = /[a-z]+/
re = /^hello.*world$/
; Escaped slash
re = /a\/b/
Flags
| Flag | Meaning |
|---|---|
g | Global match |
i | Case-insensitive |
m | Multiline mode |
s | . matches newline |
Flags are optional, following the closing /, consisting of ASCII letters.
Context-Sensitive Lexing
/ is both the division operator and the regex literal delimiter. Nolang uses context-sensitive lexing (same as JavaScript) to disambiguate:
- Expression-start positions (statement beginning, after
=/(/[/{/,/:/;etc.) →/starts a regex literal - Value-producing positions (after identifiers, literals,
)/]/}etc.) →/is division //is always a line comment (highest priority)
; Regex literal (expression-start position after '=')
re = /\d+/
result = match-text(/[a-z]+/, text)
; Division (value-producing position after identifier)
ratio = 100 / 4
x = a / b
Desugaring
Regex literals desugar at codegen into a call to the standard library regexp-compile function:
; source
re = /\d+/
; desugars to
re = regexp-compile('\\d+')
regexp-compile is defined in std/regexp.no; it creates a regexp struct and calls .compile().
Usage Examples
#{fmt-skip}
; Create a regex and match against it
re = /\d+/
matched = re.matches('hello 123 world')
print(matched) ; true
; Find a match
re = /[a-z]+/g
result = re.find('hello 42 world')
print(result) ; "hello"
; As a function argument
result = match-text(/\d+/, text)
Note: Empty pattern
//collides with line comments (same as JavaScript). Use/(?:)/for an empty match.
Naming Rules
Variable names, function names, struct names, etc. can start with an underscore, followed by hyphens, letters, and digits. They cannot start with a digit, cannot end with a hyphen, and cannot contain consecutive hyphens.
Case conventions (mandatory):
- Global constants, global variables: must start with an uppercase letter (e.g.,
NOLANG,MAX-SIZE,HEX-CHARS). Private globals use an underscore prefix followed by uppercase (e.g.,_NOLANG,_PRIVATE-CONST). A private global is visible only inside its declaring module: reading/writing it from a function in the same module is fine, but a cross-module accessmod._BUFis a hard error (drop the_to make it a public global)._means "module-private", not "unreadable by functions". - Local variables, function parameters: use lowercase letters (e.g.,
hex-chars,data-len). Do NOT use the_prefix for local variables — they are inherently private to their scope and do not need a visibility marker. The_prefix is reserved for private globals and FFI private declarations only. - Function names, struct names: use lowercase letters (e.g.,
sha1-block,db-mysql)
Function naming conventions (strongly recommended):
- Do not prefix function names with the module name. A function inside a module only needs a short, intuitive name; the module prefix is supplied automatically as
ShortName.at cross-module call sites. For example, define the entry functiontail(nottail-run) and the helperatoi(nottail-atoi) intail.no. The code stays cleaner, and cross-module calls read more naturally astail.tail(). - Entry functions are best named after the module itself (e.g.
ping.no→ping,cat.no→cat). A cross-module import looks like# /src/tail.tail. - Avoid keywords:
run(the async keyword) andmatch(the conditional-match keyword) cannot be used as function names. Pick another name for that meaning (e.g.mainis deprecated — just use the module name).
Global variables must start with an uppercase letter. This is an enforced rule, not a convention. A top-level variable starting with a lowercase letter is treated by the compiler as a local variable, which can lead to undefined-reference errors.
; ✅ Correct: global data uses uppercase letters
NOLANG = 'nolang'
MAX-SIZE = 1024
HEX-CHARS = '0123456789abcdef'
; ✅ Private globals: underscore prefix, still uppercase after it
_NOLANG = 'nolang'
_PRIVATE-CONST = 42
#{fmt-skip}
; ❌ Wrong: global variables must not start with a lowercase letter
; x1 = 10
; x = 10
; foo-bar = 42
; hello-world = 'Hello World'
; ✅ Local variables (inside functions) use lowercase, no _ prefix
; fn-example = () {
; x1 = 10
; x = 10
; foo-bar = 42
; hello-world = 'Hello World'
; ; ❌ wrong: local variables do not need _ prefix
; ; _x = 10
; }
Avoid Global Variables in Modules
Strong recommendation: Unless necessary, do NOT use global variables in modules (.no files). Global variables introduce the following issues:
- Compiler bug risk: The Nolang compiler has known limitations with cross-function memory address handling for global struct variables — different functions may see different addresses, leading to inconsistent state.
- Concurrency safety: Global mutable state is hard to track under fork or async scenarios, prone to race conditions.
- Testability: Global state creates implicit dependencies in functions, making isolated testing difficult.
- Code readability: Global variables obscure data flow — readers must trace the entire module to understand function behavior.
Recommended practices:
- Prefer local variables: Keep state in local variables within functions; pass data via parameters and return values.
- Use structs to encapsulate state: Organize related state into structs and operate via methods (method receivers are local variables with consistent addresses).
- Use global variables only when necessary: e.g., module-level constants (immutable), singleton resources (such as a global log buffer).
- Global variables MUST be uppercase: This is a mandatory rule (see above). Lowercase top-level variables are treated as locals by the compiler.
#{fmt-skip}
; ❌ Avoid: using global mutable variables in modules
; g-conn = tls.conn {}
; g-buf = ' '
;
; fn-a = () {
; g-conn.send(g-buf) ; global variable address may differ across functions
; }
#{fmt-skip}
; ✅ Recommended: use local variables, pass state via params/return values
fn-a = () {
conn = tls.conn {} ; local variable, consistent address
buf = ' '
conn.send(buf)
}
; ✅ Recommended: encapsulate the full flow in a single function to avoid cross-function state passing
serve-once = (listen-fd fd, body str) (ok bool) {
ok = false
client-fd = net.net-accept(listen-fd)
conn = tls.server-init(client-fd) ; local variable
conn: {
ok -> {
c = it
c.handshake()
c.send(body)
c.close()
ok = true
}
-> fs.close(client-fd)
}
}
Real-world example: The
https-serve-oncefunction instd/net/tls.noencapsulates the entire HTTPS request-response cycle (accept + handshake + recv + send + close) in a single function, keeping all TLS state in local variables — successfully avoiding the compiler bug where global variables have inconsistent addresses across functions.
API Documentation Conventions
A function's documentation comment should include the complete parameter names and types, and the return parameter names and types.
Rules:
- The documentation comment above a function definition must list the name and type of each parameter, and the name and type of the return parameters
- The API summary at the top of a module should also use full signatures (parameter names, types, return names, types), without abbreviated forms
#{fmt-skip}
; ❌ Wrong: missing types, missing return parameter name
; sha1(data) (hash)
; sha1-block(s, h0..h4)
; ✅ Correct: includes parameter names, types, return parameter names, types
; sha1(data []byte) (hash [20]byte) — full hash
; sha1-block(s []u32, h0 u32, h1 u32, h2 u32, h3 u32, h4 u32) — process a single block
; The documentation comment above a function definition should follow the same convention:
; sha1: compute the SHA-1 hash
; data []byte: input byte array
; returns hash [20]byte: 20-byte hash value
sha1 = (data []byte) (hash [20]byte) {
...
}
Translatable Documentation Comments (Markdown Link)
A documentation comment may end with a standalone Markdown link that points the whole comment at an entry in the translation catalog:
; get: the i-th argument
; delegates to the os.arg(i) builtin, which reads argv[i] of main; an out-of-range i returns nil
; [get](#args.get)
get = (i i64) (val ?str) {
val = os.arg(i)
}
Rules:
- The link must be the last line of the comment group, alone on it. Every line above it forms one block and is translated as a single message
- The target must be a
#anchor(an entry in the same catalog). Ordinary external links such as[docs](https://example.com)are prose written for humans and are not treated as translation markers - The anchor is the key (
args.get). The author writes it and the translator sees it in the catalog; it is not derived from the text, so rewording never orphans an existing translation. Anchors allow letters, digits,.,_and-, are global, and should carry the module prefix - The prefix is the flattened module name, not the directory. The compiler
flattens a std file to its basename:
std/collection/linked-hash-map.nois the modulelinked-hash-map, anduse std/collectionnever putscollection.in front of a symbol. So the anchor forlinked-hash-map.clearislinked-hash-map.linked-hash-map.clear(module, then the name as written), notcollection.linked-hash-map.linked-hash-map.clear. The directory segmentscollection/net/crypto/archive/encoding/databaseare not part of any symbol and must not appear in an anchor - The label (
[get]) is only a hint for whoever reads the source; the whole link line is stripped from the display.[](#args.get)is equally valid - Only
;single-line comments can carry a link (;;and;;blockcannot)
Translations live in locales/<language>-doc.noml, keyed by the anchor. A missing
translation falls back to the comment's own text rather than becoming blank. The
file name follows the .no naming rule (lowercase letters, digits, single
hyphens), and the tag is spelled the same way: --lang=zh-tw reads
zh-tw-doc.noml.
Every documented function and method in the standard library already carries an
anchor (about 2000 of them), so a translation becomes visible in the LSP the moment
it lands. make i18n writes only translated entries into the catalog — it never
copies the source text in as if it were a translation, which would put Chinese back
into the English catalog. go run ./i18n/cmd/genen -doc-missing lists the anchors
that are still untranslated.
LSP hints: hovering the function name shows the translated block; hovering the link line itself shows what the anchor points at (also in the current language), so a translation can be checked without leaving the comment.
Prefer the Standard Library
The Nolang standard library provides a rich set of common functionality, including string operations, byte conversions, hash computation, networking, and more.
Rule: If the standard library already provides the corresponding functionality, re-implementing it yourself is discouraged. Developers should carefully review the standard library documentation (docs/docs/std/overview.md) to avoid reinventing the wheel.
#{fmt-skip}
; ❌ Wrong: re-implementing str → []byte conversion
str-to-bytes = (s str) (out []byte) {
n = s.len-bytes()
i = 0
{
out[i] = s[i]
i = i + 1
} (i < n)
}
; ✅ Correct: use the standard library str.to-bytes() method
data []byte = s.to-bytes()
Common standard library replacements:
str.to-bytes()— string to byte array (replaces hand-writtenstr-to-bytes)[]byte.to-str()— byte array to string (replaces hand-writtenbytes-to-str)[n]t.to-vec()— fixed-length array to slice ([20]byte→[]byte)[]byte.to-hex()/[]byte.to-hex-lower()— byte array to hexadecimal stringstr.to-i64()/str.to-f64()— parse string to numberint.to-str()/float.to-str()— number to stringstd/crypto/sha1,std/crypto/sha256,std/crypto/sha512— hash computation
TOML Configuration Files (toml)
The toml standard-library module parses and serializes TOML 1.0 configuration files. It supports comments, bare/quoted/dotted keys, [table], [[array-of-tables]], basic/literal/multiline strings, decimal/hex/octal/binary integers, floats, booleans, date-time values, arrays, and inline tables. Values are retained as their original TOML literals so arrays and date-time values are not lost.
cfg = toml.parse('title = "Nolang"\n[server]\nport = 8080\n[[server.backends]]\nname = "local"')
doc = toml.new()
doc.put('name', '"nolang"')
doc.put('server.port', '8080')
doc.put('enabled', 'true')
text = toml.stringify(doc)
Common APIs:
toml.new()— create an emptytomltoml.parse(text)— parse TOML and return?tomldoc.put(key, raw-value)— insert or update a raw TOML value; invalid values are rejectedget(doc, key)— retrieve the raw value (?str)get-str(doc, key),get-i64(doc, key),get-f64(doc, key),get-bool(doc, key)— retrieve converted scalar valuestoml.stringify(doc)— serialize TOML; array-of-tables are emitted with[[...]]headers
The current document model supports up to 4 keys, 2 ordinary tables, and 2 array-of-tables per document. A key is limited to 32 bytes and a value to 128 bytes.
YAML Documents (yaml)
The yaml standard-library module parses and generates YAML 1.2 core-schema documents: block mappings, block sequences, flow collections, all three quoting styles, block scalars | / >, comments, multi-document streams, anchors and aliases, merge keys <<, explicit keys, and type inference (null / bool / int / float / .inf / .nan).
#{fmt-skip}
doc ?yaml = yaml.parse('name: Alice\nserver:\n port: 8080\ntags:\n - a\n - b\n')
doc: {
nil -> print('nil')
err -> print(it.msg())
-> {
doc = it
name = doc.find-str('name') ; ?str
port = doc.find-i64('server.port') ; ?i64
name: {
ok -> print(it)
-> print('')
}
print(doc.to-json())
}
}
Common APIs:
yaml.parse(text)/yaml.parse-all(text)— parse, returning?yamlyaml.valid-of(text)/yaml.error-of(text)— validation onlyit.find(path)/it.get(key)— child lookup, returning?yamlit.find-str(path),find-i64,find-f64,find-bool— typed shortcuts returning?valueit.to-json()/it.to-yaml()— serialization
Note: child views are returned as
?yamland value helpers as?value; unwrap them with a match (child = it.find(path), whoseokarm bindsitto the child and whose default arm handles the none case, one arm per line).
File Naming
.no file names (including folder names) always use hyphens - to join words, not underscores _. This is consistent with the naming style of Nolang identifiers such as variable names, function names, and struct names.
✅ Recommended:
utils/
├── string-helper.no
├── hash-table.no
└── http-client.no
❌ Avoid:
utils/
├── string_helper.no
├── hash_table.no
└── http_client.no
Function Definition
A function can emit results through named result parameters; this is essentially
sugar for out-parameters; return (and its shorthand ***, not recommended yet) is only for early termination — it cannot carry a result.
The Nolang function form is name = (in-params) (out-params) { body }: the second
parenthesis group (out-params) declares the named result parameters (the sugar). The body
assigns to them (they are reference types just like ordinary input parameters);
the variable that actually receives the result must be defined at the call site — the
result parameter names in the definition are only placeholders, and the caller's LHS
(or trailing argument) decides which variable the result lands in:
#{fmt-skip}
parse-line = (s str, max-fields ?i64) (fields []str) {
... ; the body assigns to fields
}
; The call site defines the receiving variable (LHS binding; the type is inferred from the signature)
fields = parse-line(line, nil)
; Multiple results bind in order
a, b = swap(x, y)
; Or the trailing-argument form: res is passed in as an extra output argument
add1(5, 3, res)
Nolang functions have the following characteristics:
- Named result parameters are sugar: underneath, results still travel through input parameters (out-parameters); no new return-value object is created, so it is internally safe.
- Input parameters are read-only: scalars are passed by value; composite types are passed by read-only reference. Writing to input parameters or their sub-fields inside the function body is prohibited.
- Output parameters are writable: the caller may bind an existing variable to an output slot, in which case the function modifies that memory directly; or leave the output unbound and let the function produce a fresh value.
- Variables inside a function are automatically destroyed when the function exits.
- The call site must supply a receiving variable (LHS or trailing argument). Saying "the function has no return value" is imprecise — precisely: results are passed through named result parameters (sugar for out-parameters) and are bound by the call site.
- Alias rule: an input read-only reference and an output slot may point to the same object. As long as writes occur only in the output area and inputs are only read, this is legal; the compiler does not perform static alias checking.
Parameter Default Values
Not supported. name type = expr in a parameter list (for example foo = (a i64, b ?i64 = 10)) is a syntax error (E_DEFAULT_PARAM): the compiler does not parse the default expression and never fills in a default at any call site. This removes the ambiguity of "one declaration, many legal arities" and avoids the monomorphisation blow-up caused by re-lowering a default expression at every call site.
To express optionality, declare the parameter as an option type ?T and have the caller pass nil / ok(...) explicitly; the body handles it with match:
#{fmt-skip}
parse-line = (s str, max-fields ?i64) (fields []str) {
limit = 1024
max-fields: {
ok(v) -> limit = v
nil ->
err ->
}
... ; limit is the cap
}
; The caller must pass the argument explicitly
fields = csv.parse-line(line, nil) ; falls back to 1024
fields = csv.parse-line(line, ok(256)) ; cap is 256
#{fmt-skip}
add = (a i64, b i64) (result i64) {
result = a + b ; Return the result through the parameter
... ; Placeholder statement: does nothing, dropped entirely at build time (see "Placeholder statement")
}
; Variadic parameters
add3 = (a ..i64) {
}
; Function call
sum = add(1, 2) ; sum == 3
; Anonymous function — and for? With parameters?
(a i64) { print(a) }(10)
; Function call
add(a, b)
; Multiple return values are also possible
a, b = swap(5, 3)
Control Flow
Deprecated syntax (removed after version n):
for { }/for init, cond, update { }/for i in [..) { }/match x { }/if/elif/else { }can still be parsed but will emit a deprecation warning. Please use the "new syntax" below instead.
Old vs. New Comparison
| Old / suffix (suffix) | Prefix (prefix, no fmt default) |
|---|---|
{ } (true) infinite loop | !! { } or true { } |
{ } (cond) conditional loop | (cond) { } (empty () means false, not executed) |
{ } () not executed | ! { } or false { } or () { } |
{ } * n constant count | n * { } (N ≤ 0 skips the body) |
for i=0, i<n, i++ { } counting | n * { } (constant count) or i <- [0..n): { } (variable) |
for i <- [a..b] { } range | i <- [a..b]: { } |
for i in [a..b) { } range | i <- [a..b): { } |
match x { ... } matching | x: { ... } |
if/elif/else { } branching | { cond -> body } |
continue | * (shorthand not recommended yet) |
break | ** (shorthand not recommended yet) |
return | *** (shorthand not recommended yet) |
Loop / While / for-in
Two equivalent spellings: every loop supports a prefix form
(cond) { }and a suffix form{ } (cond)— the semantics are identical, only the order of condition and body differs.no fmtemits the prefix form by default; useno fmt -loop-style=suffixto switch back (both spellings are format-idempotent).
#{fmt-skip}
; === Prefix form (condition before the body) ===
; Infinite loop (condition is always true) — these two are equivalent
!! {
...
}
true {
...
}
; Not executed (condition is always false) — these three are equivalent
! {
...
}
false {
...
}
() {
...
}
; Conditional loop (checks cond, runs the body while true)
(x == 1) {
do-something()
}
; Limited execution count (prefix counted loop)
10 * {
do-something()
}
; When N <= 0 the loop body does not execute (zero or negative count is skipped)
0 * {
print('will not execute')
}
-3 * {
print('will not execute either')
}
; A label may be attached (the label goes before the condition / count)
#1 (x == 1) {
do-something()
}
; === Suffix form (condition after the body, legacy) ===
{
...
} (true)
; Not executed (empty parens mean false)
{
...
} ()
; Conditional loop
{
do-something()
} (x == 1)
; Limited execution count
{
} * 10
; When N <= 0 the loop body does not execute (zero or negative count is skipped)
{
print('will not execute')
} * 0
{
print('will not execute either')
} * -3
; Range syntax (i64/u8 and other numeric types, arr, vec and str are supported)
; 16 combinations in total: '[' includes the left endpoint, '(' excludes it; ']' includes the right endpoint, ')' excludes it.
; Bounded (4): both ends have a value
i <- [a..b]: { ; closed interval: a ≤ i ≤ b
}
i <- (a..b]: { ; left-open right-closed: a < i ≤ b
}
i <- [a..b): { ; left-closed right-open: a ≤ i < b
}
i <- (a..b): { ; open interval: a < i < b
}
; No upper bound (4): the right endpoint is omitted, iterating to the type's maximum
i <- [a..]: { ; a ≤ i ≤ type max
}
i <- [a..): { ; a ≤ i < type max
}
i <- (a..]: { ; a < i ≤ type max
}
i <- (a..): { ; a < i < type max
}
; No lower bound (4): the left endpoint is omitted, starting from the type's minimum
i <- [..b]: { ; type min ≤ i ≤ b
}
i <- [..b): { ; type min ≤ i < b
}
i <- (..b]: { ; type min < i ≤ b
}
i <- (..b): { ; type min < i < b
}
; Fully unbounded (4): both ends omitted, based on the type's minimum/maximum
i <- [..]: { ; type min ≤ i ≤ type max
}
i <- [..): { ; type min ≤ i < type max
}
i <- (..]: { ; type min < i ≤ type max
}
i <- (..): { ; type min < i < type max
}
i <- [5..0]: { ; decrement — runtime direction detection: start > end → decrement
}
i <- 'abc': { ; iterate over each character in the string
}
; Runtime direction detection: when start > end, iteration automatically decrements (step -1).
; All four bracket combinations support decrement:
; [5..1] → 5 4 3 2 1 left-closed right-closed, descending
; (5..1] → 4 3 2 1 left-open right-closed, descending
; [5..1) → 5 4 3 2 left-closed right-open, descending
; (5..1) → 4 3 2 left-open right-open, descending
; (3..0] → 2 1 0 left-open right-closed, descending to zero
; When start <= end, iteration increments as usual (step +1).
;
; The endpoints of an unbounded range come from the variable's static type:
; [..] on i64 from i64.MIN (-9223372036854775808) to i64.MAX (9223372036854775807)
; [..] on u8 from 0 to 255
; [..) on i64 from i64.MIN to i64.MAX - 1 (maximum excluded)
; (..] on i64 from i64.MIN + 1 to i64.MAX (minimum excluded)
; (..) on i64 from i64.MIN + 1 to i64.MAX - 1 (both ends excluded)
; Rule: '[' includes the type minimum, '(' starts at minimum+1;
; ']' includes the type maximum, ')' stops at maximum-1.
; ❌ Explicitly rejected
; Range bounds must be integers; nested expressions are not supported
; for i <- [1.5..5.5] { } ; compile error
; for i <- [0..[1..5][0]] { } ; syntax error
; ⚠️ Avoid the ... ambiguity
; The range operator is .. (two dots). The self-method call is .len().
; When written without a space: [0.. .len()) → [0...len()), the three dots
; look like a single operator (the return shorthand is now *** and is not recommended yet).
; Use self.len() instead of .len() to disambiguate: i <- [0..self.len()): { }
; (self and . are semantically equivalent inside method bodies)
; Conditional loop (new style: prefix (cond) { }, the `no fmt` default; replaces the old for cond { })
; The suffix form { } (cond) is fully equivalent (legacy spelling)
; In most cases, range-for can be used instead: i <- [0..n): { }
(x == 1) {
do-something()
}
Break / Skip / Early Return
⚠️ The
*(skip iteration) /**(break loop) /***(return/terminate) shorthands are not recommended yet — write the English keywordscontinue/break/return;no fmtnormalizes the supported shorthand back to keyword form.
#{fmt-skip}
i <- [0..10): {
* ; continue (skip this iteration; shorthand not recommended yet)
** ; break (exit loop; shorthand not recommended yet)
*** ; return/terminate (early return; only terminates the function; planned, not recommended yet)
}
; The English keywords are the recommended form (consistent with C/Rust, which helps when porting code — they compile and run normally):
i <- [0..10): {
continue ; equivalent to * (not recommended yet)
break ; equivalent to ** (not recommended yet)
return ; equivalent to *** (not recommended yet), only terminates the function early
}
; Keyword aliases (full lexer support, exactly equivalent to the long forms):
; ctn ≡ continue, brk ≡ break, rtn ≡ return; `no fmt` keeps the alias spelling verbatim
i <- [0..10): {
ctn ; equivalent to continue
brk ; equivalent to break
}
rtn ; equivalent to return, only terminates the function early
⚠️
return(and its shorthand form***, which is not recommended yet) can only be used bare — it cannot carry a return value. Nolang functions have no "return value" mechanism — results are always handed out by assigning to named result parameters (out-params) inside the body (see "Function Definition"). Soreturn <value>,return(expr)and*** <value>are all forbidden, and the compiler / formatter / LSP all report them:
- Compiler (
no build):Error: compilation error: parser errors: line L, column C: 'return' 後不能跟返回值;… [E_GENERAL]no vet:<file>:L:C: [ERROR] nolang-compile: 'return' 後不能跟返回值;… [E_GENERAL](same format as the other diagnostics, one line each; the message text itself is emitted in Chinese)no fmt:format error: …and exits with a non-zero status- LSP: a red error diagnostic on the offending line
Wrong:
has = (n i64) (r i64) {n == 0 -> return 0 ; ❌ compiler / formatter / LSP all report an errorr = n}Correct (assign the result parameter first, then use a bare
returnto terminate early; the statements inside an arm body each need their own line):has = (n i64) (r i64) {n == 0 -> {r = 0return}r = n}⚠️ Collapsing it to one line as
n == 0 -> { r = 0; return }is not a shorthand:; return }is entirely comment, so the block never closes and every following statement (usually the rest of the function body) is silently swallowed into that arm — verified:no vetreports no issue and the function is never called.
Placeholder statement (...)
... is a placeholder statement: syntactically valid, semantically it does nothing. It is
meant for documentation sketches, code skeletons and TODO slots. At compile time the whole
statement is dropped during AST→HIR lowering, so the output is identical to never having written
the line (verified: two binaries built from the same source with and without ... have the same
sha256).
parse-line = (s str) (fields []str) {
; TODO: handle escape sequences here
...
fields = csv.parse-line(s)
}
add = (a i64, b i64) (result i64) {
result = a + b
...
}
Rules:
- It must be the only thing on its line (a trailing
;comment is allowed). Forms such asfail -> ...,x = ...or[0...n)are syntax errors —...has no value, so it cannot appear in expression position or anywhere else inside a statement. - It is not an alias for
return, and not a range operator: ranges use..(two dots), early return usesreturn. no fmtkeeps it verbatim — neither deleting nor moving it.
None of the following is valid:
#{fmt-skip}
add = (a i64, b i64) (result i64) {
result = a + b
... ; ✅ the only legal form: alone on its line
}
ok -> ... ; ❌ syntax error: `...` cannot appear in expression position
x = ... ; ❌ syntax error: same
i <- [0...n): { ; ❌ syntax error: `...` is not a range operator, write [0..self.n)
}
Match
#{fmt-skip}
; Simple form; it is used to access the argument
x: {
err -> log(it.msg()) ; it is the structured error err.err
nil -> log('nil')
->
do-right-thing(it)
}
; Destructuring form
x: {
err(e) -> log(e.msg())
nil -> log('nil')
ok(v) ->
do-right-thing(v)
}
; Note: struct pattern matching (`user: { User{id=1} -> ... }`) and field binding are not supported yet, so they are not shown here
score: {
[0..59] -> print('fail')
[60..89] -> print('good')
[90..100] -> print('excellent')
-> print('invalid score')
}
; Range syntax has 16 combinations in total. The rule: '[' includes the left endpoint, '(' excludes it;
; ']' includes the right endpoint, ')' excludes it.
;
; [Bounded] (4) — both ends have a concrete value
; [a..b] → x >= a && x <= b both ends inclusive
; [a..b) → x >= a && x < b left inclusive, right exclusive
; (a..b] → x > a && x <= b left exclusive, right inclusive
; (a..b) → x > a && x < b both ends exclusive
;
; [No upper bound] (4) — right endpoint omitted (End=nil); only the lower bound is checked
; [a..) → x >= a left inclusive, no upper bound
; [a..] → x >= a left inclusive, no upper bound
; (a..) → x > a left exclusive, no upper bound
; (a..] → x > a left exclusive, no upper bound
;
; [No lower bound] (4) — left endpoint omitted (Start=nil); only the upper bound is checked
; [..b] → x <= b no lower bound, right inclusive
; [..b) → x < b no lower bound, right exclusive
; (..b] → x <= b no lower bound, right inclusive
; (..b) → x < b no lower bound, right exclusive
;
; [Fully unbounded] (4) — both ends omitted, based on the type's minimum/maximum
; [..] → true type min ≤ x ≤ type max (both ends inclusive)
; [..) → true type min ≤ x < type max
; (..] → true type min < x ≤ type max
; (..) → true type min < x < type max
;
; In a match, a fully unbounded range is based on the matched variable's type domain (necessarily true, equivalent to a catch-all).
; In range-for it is based on the iterator variable's type range (e.g. [..] on i64 runs from i64.MIN to i64.MAX).
;
; Example: using unbounded ranges for a complete classification
v = 85
result = v: {
[..0) -> 'negative'
[0..60) -> 'fail'
[60..80) -> 'pass'
[80..90) -> 'good'
[90..100]-> 'excellent'
(100..) -> 'extraordinary'
}
print(result)
num: {
1 || 3 || 5 || 7 -> print('small odd number')
2 || 4 || 6 -> print('small even number')
-> print('larger number')
}
; Has a return value; the last statement/value
result = x: {
1 -> 1
2 -> 2 + 1
-> a + b
}
; Multi-line arm bodies must use braces: -> { ... }
x: {
nil -> {
log('nil')
do-cleanup()
return
}
err -> {
log(it.msg())
do-cleanup()
return
}
ok -> print(it)
}
Multi-line arm body rule: When an arm body contains multiple statements, it must be enclosed in braces
-> { ... }. A single-line body can be written directly after->. This is because if a multi-line body does not use braces, theitbinding of an option match cannot be inserted correctly, resulting in a compile error.
Match semantics inside a for-in body:
i <- (a..b]: { 1 -> ... 2 -> ... }executes the match body once for each iteration variablei(1 ->is equivalent toi == 1 ->, and so on). This is syntactic sugar for executing a match once per iteration.
The Three Match Subject Forms
A match subject can be a value, an expression, or a block:
| Form | Syntax | How it is evaluated |
|---|---|---|
| Value | x: { ... } | Matches x directly |
| Expression | x + 1: { ... } | Evaluates the expression, then matches its value |
| Block | { ... }: { ... } | Runs a statement body and matches the outcome of ?= inside it |
The first two are expressions: they work as statements and also as values
(v = x: { ok -> it, -> 0 }). The third is a statement only — it is not an
expression, cannot be assigned, and has no block-tail value; -> _ discards the
result. no fmt round-trips }: { verbatim, with the same blank-line rule between
arms as any other match.
Block subject (}: {)
A block subject lets a function without a ?T result parameter handle ?=
propagation locally:
; d i64: divisor; 6/d yields err when d is 0
f = (d i64) {
{
v ?= 6 / d; no ?T result param needed here
print(v.to-str()); skipped on failure
}: {
err -> print(it.msg())
nil -> print('nil')
-> _; discard the result
}
print('continues afterwards'); flow resumes after the arms
}
Semantics:
- A
?=inside the body is captured by the nearest block match (innermost wins for nested ones; a block match also wins over an enclosing function that has a?Tresult parameter). Capture is recursive: bare blocks, if bodies, loop bodies and match arms are all included. A function definition is a boundary; a function literal is not. - The subject is a three-state
?Tand uses the standard option-match exhaustiveness check: without a catch-all arm, even the unreachableok ->must be written alongsidenilanderr. - Completing the body (zero
?=, or all of them successful) matchesnil; a failed?=matches that failure'snilorerr(itin theerrarm is the fullerr.errpayload). - One failure stops the entire body: later statements and later loop
iterations do not run, and the arms execute exactly once. This stops a block,
it is not a function
return— the user's ownreturn/break/continuebindings are unaffected. - Expansion uses only existing core AST (a synthetic
?Tlocal, anif-guard chain and an ordinary match). No new HIR / MIR kinds. Synthetic names never appear in diagnostics or inno fmtoutput.
Versus
=capture-in-place:=stores the failure in the target variable and keeps going;}: {funnels the failure into one match and stops the rest of the body. Use=plus a later match when you want to continue after a failure; use}: {when a failure should jump straight to the cleanup arms.
Match Style Guide
#{fmt-skip}
; ❌ Avoid duplicated branch bodies
w = tls-c.send(req)
w: {
nil -> {
tls-c.close()
return
}
err -> {
tls-c.close()
return
}
ok -> n = it
}
; ✅ Put common logic in the -> catch-all
w = tls-c.send(req)
w: {
ok -> n = it
-> {
tls-c.close()
return
}
}
; ✅ Or vice versa: name the simple branches, put complex logic in ->
val: {
nil -> return
err -> log(it.msg())
-> {
n = it
total = total + n
process(n)
}
}
; Single statement — no braces
val: {
ok -> print(it)
-> print('empty or error')
}
; Multiple statements — braces required
val: {
ok -> {
n = it
total = total + n
}
-> {
log('failed')
return
}
}
#{fmt-skip}
; Implicit it binding
val: {
ok -> process(it) ; it = unwrapped value
err -> log(it.msg()) ; it = structured error err.err (not str)
-> log('empty') ; catch-all; here it is nil
}
#{fmt-skip}
; it scoping: nested matches restore level by level
; (the placeholder function is named keep -- `use` is the import keyword and cannot be
; a function name; and a bare `->` arm may not read `it` unless nil/err are both claimed,
; so every level here writes `ok ->`)
outer: {
ok -> {
keep(it) ; outer it
inner ?T = f(it)
inner: {
ok -> keep(it) ; inner it = unwrapped `inner`
}
keep(it) ; ✅ it is the outer value again
}
}
Note: restoration only happens for nested matches. After the outermost match ends,
itstill holds the last arm's value — do not useitoutside a match. To carry the value across levels, copy it into a named local (doc = it) or use a destructuring bindingok(v) -> ...to bind the payload to your own name.
Best practice — prefer
itfor option unwrapping: when the matched value is an option?t, writeok -> ...(it)...rather thanok(v) -> ...(v).... Introducing no extra binding name naturally sidesteps the "same-named local across arms corrupts codegen" pitfall. Keep a named bindingok(v) ->only when: (1) the scrutinee is a user-defined enum variant (res { ok(v str) },shape { rect(w, h) }) — thereitis the whole enum, not the payload, so the payload can only be extracted by name; or (2) the value is needed after a nested match — the inner match rebindsit, so use a named binding or copyitinto a named local first.
An iteration variable is also an element place
x <- a (where a is an arr / vec / slice / fixed array) binds x to the element's
place, not to a per-iteration copy:
- Reading
xre-readsa[__i], so a read after a write in the same iteration sees the new value —x = x + 1is a read-modify-write; - Writing
x = vstores back intoa[__i]; itinside a match arm is an alias of the same place (valid only within that arm), sox: { ok -> it = v }anda[i]: { ok -> it = v }are exactly equivalent.
; Both loops below leave every element of the container equal to 5.
a []i64 = [0, 0, 0]
i <- [0..3): {
a[i]: {
ok -> it = 5
-> _
}
}
b []i64 = [0, 0, 0]
x <- b: {
x: {
ok -> it = 5
-> _
}
}
- When the element is itself an option (
[]?T),itis the payload; writing back wraps it inokagain. - The alias dies with the arm:
it = vafter the match does not reach the container.
Not places (a write stays local):
c <- sover astr: the element is a decoded code point; a string has no storage cell for it.k <- mover a map: the subscript is a key, not a position, so a write does not go back into the table.
it is only usable in an arm with a single provable case
A catch-all -> arm receives every case the explicit arms did not claim. it has one
unambiguous meaning only when exactly one case remains:
; ✅ -> is ok (nil and err are both claimed)
v: {
nil -> log('empty')
err -> log(it.msg())
-> process(it)
}
; ✅ -> is err (ok and nil are both claimed)
v: {
ok -> process(it)
nil -> log('empty')
-> log(it.msg())
}
#{fmt-skip}
; ❌ -> may be nil or err — compile error
v: {
ok -> process(it)
-> log(it)
}
Add the missing nil -> / err -> arm, or switch to ok ->. Enum matches and bare matches
({ cond -> ... }) are exempt: it there is the matched value itself, with no nil/err case.
In the
err ->armitis always the error value (err.err), regardless of the option's element type — the builtin isoption { ok(v t), nil, err(e err.err) }. So even for a scalar option such as?i64/?bool,itin theerr ->arm is a structured error: read the message withmsg = it.msg(), test the code withit.is(code.io), and find where it was raised init.file/it.line/it.col(filled in by the compiler at theerr(...)site).
; ✅ Combined option pattern: nil || err -> body
; When the option is nil or err, share the same branch
val: {
nil || err -> {
cleanup()
return
}
ok -> process(it)
}
; ✅ Can also be mixed with a -> catch-all
val: {
nil || err -> log('failed')
ok -> process(it)
}
Short-Circuit Pipeline (->)
-> is a left-associative short-circuit pipeline operator. Its meaning is fixed and does not depend on the surrounding context — a bare statement, a single-line match-arm body, and the right-hand side of an assignment all lower through the same code path.
Nodes inside a pipeline fall into three classes:
| node | effect on the pipeline state |
|---|---|
side-effect call with no return (print('A')) | runs; does not change state — execution continues to the next node |
call returning ?T (fallible, e.g. might-fail()) | runs; overwrites state — once it yields nil/err, every later node is skipped |
| trailing plain value | evaluated only while the state is still ok |
Only a node returning ?T can put the pipeline into a failed state; a print never can. A chain made entirely of print calls looks like short-circuiting is off — it is not; there is simply no node capable of failing.
; no fallible node -> everything runs
ok -> print('A') -> print('B')
; might-fail() returns ?T -> on failure print('B') is skipped
ok -> print('A') -> might-fail(x) -> print('B')
; same semantics on the right-hand side of an assignment
r = print('E') -> might-fail(x) -> 42
Boundary rules:
->has exactly one meaning. Bare statements and assignment expressions share one pipeline lowering; wrapping in an assignment does not switch modes.- A
->inside a match-arm body continues that arm's pipeline; it does not start a new arm. Arms are separated by newlines:
; ✅ one arm whose body is a pipeline -> both print
ok -> print('A') -> print('B')
; ✅ two arms (newline-separated) -> 'miss' does not print when ok matched
v: {
ok -> print('hit')
-> print('miss')
}
⚠️ Because arms are newline-separated,
pat -> A -> Bwritten on ONE line is one arm with a pipeline, not "if A else B". For if/else, put the arms on separate lines or use a{}short-circuit group.
- A pipeline can produce a value. On the right-hand side of an assignment the pipeline's result is its trailing value node (evaluated only while the state is still ok).
#{fmt-skip}
n = 7
x = 1 > 2 -> 42 ; condition false -> x keeps its previous value, 7
y = 2 > 1 -> 42 ; y == 42
s str = 'old'
s = 1 > 2 -> 'new' ; short-circuited -> s is still 'old'
r = print('E') -> might-fail(bad) -> 99 ; middle node failed -> r keeps its previous value
When the pipeline fails (false condition, or a ?T node that short-circuited) the assignment does not happen: the variable keeps its previous value, or the type's zero value if this is its first binding. That matches the statement form — { 1 > 2 -> x = 42 } also leaves x alone.
The value's type is inferred from the arm's trailing expression (str / i64 / ?T all work), sharing the inference used by the existing match-as-value form x = subject: { arms }.
If / Else
If-else groups (short-circuit) must be wrapped in {}. The first matching condition wins; later conditions are not checked.
; Multiple branches (new style recommended) — short-circuit if-else chain
{
a == 1 -> {
a = 1
b = 2
}
a == 2 || a == 3 -> do-something()
-> {
c = 0
}
}
; Single if (retained) — standalone, no short-circuit
x == 1 -> do-something()
; Ternary expression: condition ? true-value : false-value
c = flag ? 1 : 2
max = sum > 10 ? sum : 10
Key rules:
- Short-circuit group — Multiple
cond -> bodywrapped in{}form an if-elif-else chain. Only the first matching branch executes. - Standalone if — Writing
cond -> bodydirectly in a function/loop body (without{}) is an independent if. It does not short-circuit with adjacent if-then lines. An else forces the group. A newline->else arm written outside{}is a compile ERROR —k3r7mbqdfor a lonecond ->plus->, andm7vq3xrtwhen siblingcond ->arms sit above it (the message names how many arms are ignored). The parser folds a newline->onto only the nearest preceding standalone if, so every arm above it is silently dropped: what the code reads as is not what it compiles to, which is why this is a rule rather than a warning. Wrap every arm in{ ... }; a lonecond -> bodywithout an else may stay ungrouped. - No mixing — Inside a
{}short-circuit group, all direct children must becond -> bodyarms. Regular statements are not allowed as direct children; place them inside branch bodies.
; ❌ No short-circuit — independent ifs, all conditions checked
git-dispatch = (cmd str) {
cmd == 'a' -> {
fa()
}
cmd == 'b' -> {
fb()
}
}
; ✅ Short-circuit — wrapped in {}, first match wins
git-dispatch = (cmd str) {
{
cmd == 'a' -> {
fa()
}
cmd == 'b' -> {
fb()
}
true -> {
}
}
}
#{fmt-skip}
; ❌ if/else written outside the group — compile ERROR
; the newline `-> break` binds to `c == 43` only, so a matched `c == 45` is still cut short
; c == 45 -> {
; exp-sign = -1
; i = i + 1
; }
; c == 43 -> i = i + 1
; -> break
; ✅ Same code as an if-group — arms are exclusive, `->` is a real fallback arm
{
c == 45 -> {
exp-sign = -1
i = i + 1
}
c == 43 -> i = i + 1
-> break
}
#{fmt-skip}
; ❌ Match-block with mixed regular statement → compile error
{
cmd == 'a' -> { fa() }
print(cmd)
cmd == 'b' -> { fb() }
}
; ✅ Regular statement moved into branch body
{
cmd == 'a' -> {
print(cmd)
fa()
}
cmd == 'b' -> {
fb()
}
true -> {
}
}
Async Programming
Nolang has one surface form for concurrency: an awy { ... } coroutine group with
co <call> statements inside. See "Asynchronous Programming" below for the full
syntax. run is no longer a keyword.
Multiple Assignment
Functions can return multiple values; use multiple assignment to receive them when calling:
#{fmt-skip}
; Function definition returning multiple result parameters
swap = (a i64, b i64) (x i64, y i64) {
x = b
y = a
}
; Multiple assignment
a, b = swap(5, 3)
; Use _ to ignore return values you do not need (placeholder variable)
_, b = swap(5, 3) ; take only the second value, ignore the first
a, _ = swap(5, 3) ; take only the first value, ignore the second
_, _ = swap(5, 3) ; ignore every return value (the call is made for its side effects)
; Also supported as the body of a match arm
val: {
ok -> a, b = parse-pair(it)
-> return
}
Asynchronous Programming: awy { ... } + co
Nolang has a single surface syntax for concurrency: an awy { ... } coroutine
group containing co <call> statements. run is no longer a keyword (it is a
plain identifier); awy <handle> and bare-block coroutine groups are gone.
co outside a group is a compile error (E_CO_OUTSIDE_GROUP).
; a procedure: it only writes the channel, it declares no result
produce = (ch chan[i64], v i64) {
ch <- v
ch.close()
}
ch chan[i64] = with-cap(2)
awy {
co produce(ch, 1) ; (1) procedure spawn: co f(args)
co { ; (2) closure spawn: co { ... }
ch <- 2
ch.close()
}
}
sum = 0
i <- ch: {
sum = sum + i
}
print(sum.to-str()) ; => 3
(1) Procedure spawn: co f(args)
f must not declare a result. A spawn has nowhere to put one, so a
value-returning function passed to co is rejected with c0retval:
worker = (n i64) (r i64) { r = n } ; declares a result
awy { co worker(1) } ; ✗ c0retval
A method's implicit self does not count: it is the receiver out-param every
method carries so calls can chain, so co obj.flush() is an ordinary spawn:
queue {
n i64
}
queue.flush = () { ; implicit (self queue), no other result
self.n = 0
}
awy { co q.flush() } ; ✓
When you need the value, use a channel: the procedure sends it and close()s,
and the caller drains the channel after the group. This is a rule, not a style
choice — a spawned call's value only becomes readable once the group is joined,
and the reader is not at the join point; chan is exactly the hand-off for that.
(2) Closure spawn: co { ... }
co { ... } hands a block to a coroutine as a procedure. The block's free
variables are captured by value (a snapshot at the spawn, the same semantics as
the deep copy of a spawn argument):
seed = 33
ch chan[i64] = with-cap(1)
awy {
co {
ch <- seed
ch.close()
}
}
r = 0
<- ch: { ok -> r = it }
print(r.to-str()) ; => 33
co is not a value
co produces void, so it must fill a whole statement. A binding's right-hand
side, an argument position and a return position are all rejected with
E_CO_NOT_STATEMENT (c0nostmt):
r = co produce(ch, 1) ; ✗ c0nostmt
print(co produce(ch, 1)) ; ✗ c0nostmt
And co may only be followed by a call or a block; anything else is
E_CO_NOT_CALL (c0notcal).
co (colorless async)
co is colorless syntax: you write the uncolored name produce and the compiler
synthesizes a produce-async variant that runs on the underlying stackless
coroutine machinery. Do not hand-write the -async suffix.
- Monomorphization.
co produce(ch, 1)generatesproduce-async(a renamed clone). Ifproduceis also called synchronously, both variants coexist. - Transitivity. If a procedure itself contains
awy { co child(n) }, it too is monomorphized into-async; coloring propagates up the call chain. - Barriers. Any non-
costatement in the group (an assignment, aprint, a loop, anif, a channel read) waits for in-flight tasks first. A data dependency inside a group is therefore written as "read the channel, then spawn with the value you read" — the read is the barrier, and it says what it means. - Function bodies are not groups. Only
awy { ... }introduces one; acoin a function body,ifarm or loop body is rejected. - Discarded results are still awaited.
awy { co side(5) }leaves no task running at scope exit.
Lowering detail (not source syntax): each
cobecomesOpRun, and each task gets anOpAwaitat group exit.KRun/KAwait/OpRun/OpAwaitexist only in HIR/MIR.
Cancellation: cancelled() remains a cooperative self-check inside a coroutine
body. cancel(h) is a low-level handle operation with no surface handle under
the new syntax; application code does not use it.
Operator Overloading
When the static type of the left operand defines a designated _op method, the compiler rewrites the infix expression a op b into the method call a._op(b) at the lowering stage. The overload set is a closed group of five symmetric binary arithmetic operators:
| Operator | Overload method |
|---|---|
+ | _add |
- | _sub |
* | _mul |
/ | _div |
% | _mod |
#{fmt-skip}
big {
v i64
}
; the result type is written ?big but treated by the compiler as plain payload big
big._add = (b big) (c ?big) {
c = big {
v: .v + b.v
}
}
f = (a big, b big) (out big) {
r big = a + b ; rewritten by the compiler into a._add(b)
out = r
}
Type convention: the result parameter of an operator method is written as ?T but takes effect as the pure payload T — the compiler performs no option boxing or unwrapping; inside the method body a plain T value is assigned to c, and callers receive T as well. Errors such as division by zero trap at runtime exactly like the builtin numeric types; no option value is produced.
Key semantics:
- The rewrite only fires when the left operand's static type defines the corresponding
_opmethod; builtin numeric types (i64, f64, ...) keep builtin arithmetic. - An explicit method call
a._add(b)is fully equivalent toa + b. - Left-nested chains are rewritten layer by layer:
a + b + c→(a._add(b))._add(c); the intermediate result staysTand can keep participating in operators or method calls. - The standard library
bigintgains+ - * / %support through this mechanism (see the bigint section under "Standard Library > Others").
Arrays and Slices
Containers store copies of data; the original variable and the container are independent, eliminating dangling references.
Fixed-length array arr:
#{fmt-skip}
; Using a fixed-length array
a [3] = [1, 2, 3] ; fixed-length array of i64 with length 3
a [3]u16 = [1, 2, 3] ; fixed-length array with explicit type
a [?]u16 = [1, 2, 3] ; length automatically inferred (explicit type)
a [?] = [1, 2, 3] ; length automatically inferred (i64)
Variable-length array vec:
#{fmt-skip}
v = [1, 2, 3] ; variable-length array of i64
bs = [0x11, 0x22, 0x33]
v []u8 = [1, 2, 3] ; variable-length array with explicit type
; Pre-allocation (avoid repeated reallocation)
v = with-cap(100) ; len=0, cap=100 (push before indexing)
v = with-len(100) ; len=100, cap=100 (direct indexing)
v = with-cap-len(200, 100) ; len=100, cap=200 (reserved growth space)
Slice (view, not an independent type):
A slice is a view of the original data; it does not copy data and does not produce a new independent type. Internally, a slice only records a pointer to the original buffer, a length, and a capacity, so:
- Modifying elements through a slice affects the original data, and vice versa
- A slice does not own data; once the original variable is released, the slice becomes invalid
- The slice's type is determined by the original type, and methods apply naturally without an "inheritance" mechanism
#{fmt-skip}
; Supports arr/vec/str
; Supports ranges, consistent with for <- notation
nums [5]u8 = [0, 1, 2, 3, 4]
nums[..] ; [0 1 2 3 4] fully unbounded, equivalent to the whole slice
nums[1..] ; [1 2 3 4] no upper bound, from index 1 to the end
nums[..4] ; [0 1 2 3 4] no lower bound, from the start to index 4
nums[2..3] ; [2 3] bounded, both ends inclusive
nums[1..3] ; [1 2 3] bounded, both ends inclusive
nums[1..3) ; [1 2] left-closed, right-open
nums(1..3) ; [2] both ends exclusive
; String (the slice result has type str view and shares the underlying memory; indices are code-point positions, consistent with s[i])
s = 'abc'
s[1..] ; 'bc'
s[1..s.len()) ; 'bc'
b str = s[0..1] ; the slice result is already str, so it can be assigned to a str variable directly
Slice syntax supports the same 16 range combinations as match/range-for. In a slice context, unbounded ranges are relative to the slice's length:
[..]means the whole slice,[a..]means from index a to the end, and[..b]means from the start to index b.
Types and methods of slices:
A slice does not generate a new independent type; it is just a view of the original type (with an adjusted starting pointer and length). Therefore, the methods of the original type are directly available:
| Original type | Slice view type | Available methods |
|---|---|---|
arr ([n]t) | []t<range> | All methods of []t (e.g., len, push, pop, contains, reverse, clone, fill, to-arr, etc.) |
vec ([]t) | []<range> | Same as above |
str | str<range> | All methods of str (e.g., to-upper, to-lower, index, contains, slice, copy, fill, etc.) |
#{fmt-skip}
; arr slice → vec view, sharing arr's underlying memory
a [5]u8 = [0, 1, 2, 3, 4]
s = a[1..4] ; s is a []u8 view pointing into a's memory
n = s.len() ; slice.len()
; vec slice → vec view, sharing vec's underlying memory
v = [10, 20, 30, 40, 50]
s = v[2..] ; s is a []i64 view
s.reverse(s.len()) ; slice.reverse
; str slice → str view, sharing str's underlying memory
s = 'Hello World'
sub = s[6..] ; sub is a 'World' view
upper = sub.to-upper() ; str.to-upper
; Modifying elements through a slice affects the original data
data = [10, 20, 30, 40, 50]
view = data[1..4] ; view = [20, 30, 40]
view[0] = 99 ; modify an element of view
; data[1] is now also 99, because view shares data's memory
Indexing
#{fmt-skip}
; String indexing → returns ?char (a Unicode code point, not a byte)
c char = str[i] ; explicit plain type pulls the result back to char
; Get an element from arr or vec
arr[i] ; → ?elem
vec[i] ; → ?elem
; Get a value from a map → ?V (a missing key yields nil)
m[k] ; same as m.get(k)
; Write into a map — no unwrapping needed: a key has no out-of-range case
m[k] = v ; same as m.put(k, v)
An index read always has type
?T:arr[i]/vec[i]/ fixed-length arrays /slice→?elem, and indexing astr/txts[i]→?char(D6 — the same unified rule as integer arithmetic). The inferred type ofstr[i]is therefore?char; to get a plaincharback, either write an explicit plain type on the binding (c char = s[i], which short-circuits during lowering), or unwrap it with a match /?=. The slicestr[a..b]still returnsstr(a code-point-semantics view that shares the underlying memory). Acharconverts implicitly tostr(encoded as a single-character string), so all of the following are legal — there is no need to callchar.to-str()by hand:
#{fmt-skip}
s = 'héllo'
c char = s[1] ; 'é' (a char whose code point is 233)
a str = s[1] ; 'é' (implicit char → str conversion)
b str = s[0..1] ; 'hé' (the slice result already is str)
msg = 'first: ' - s[0] ; concatenating (-) a char converts it to str implicitly
ok = s[0] == 'h' ; comparing a char with a str converts it to str implicitly
A subscript write must be explicitly unwrapped
An index read returns an option (nil when out of range), so assigning straight to a subscript would turn an out-of-range index into an unchecked out-of-range write. That is therefore not allowed; pick one of two forms:
#{fmt-skip}
; ❌ Not allowed: a[1] = 2
; ✅ place-match: handle the out-of-range arm yourself (it = the element's place, readable and writable)
a[1]: {
ok -> it = 2
-> _
}
; ✅ propagate the error: inside a function that has an option result param (out of range → propagate)
a[1] ?= 2
- When the compiler can prove the index is in bounds (fixed-length array + constant index, loop variable provably in range, …) it removes the check internally and assigns directly, with no runtime cost; syntactically you must still write the unwrapping form — there is no user-side exemption.
- Maps (
[str]i64etc.) are completely exempt from this rule; see Map Subscripts below for the read/write asymmetry and them[k] ?= vtrap.
Map Subscripts
A map index is a key, not a position, so the two sides follow different rules:
- Read — same unified
?Trule as every other container:x = m[k]has type?V, andnilwhen the key is absent.m[k]andm.get(k)are the same operation. - Write —
m[k] = vis written directly, with no unwrapping: a write is a put/insert (insert when absent, overwrite when present), so it is defined for every key and there is no "out-of-range write" to guard.m[k] = vandm.put(k, v)are the same operation. This is the one exemption from "a subscript write must be explicitly unwrapped" — that rule exists because an array/vec write can be out of range, which cannot happen for a key. m[k] ?= vis a compile error, not merely redundant: the?=ok-arm means "read the element, then write back to that position", but the?Vread from a map is only a copy fromget, so writing it back does not reach the map. Writem[k] = vinstead. The place-match formm[k]: { ok -> it = v }is still accepted (equivalent to put).
#{fmt-skip}
m [str]i64 = {}
m['a'] = 1 ; insert (same as m.put('a', 1))
m['a'] = 2 ; overwrite
v ?i64 = m['a'] ; read (same as m.get('a'))
Safe Indexing
For an index v[i] on arr / vec / slice ([]T) / fixed-length arrays / str / txt, the result is always ?T: out of range yields nil, in range yields ok(value) — it never aborts (runtime error: index out of bounds). This is the same rule as the unannotated overflow behaviour of integer + - * / % (see "Integer Overflow").
There are only three ways to consume that ?T. #{index-out=DEF} was removed in D11 —
a leftover annotation is a compile error, and there is no "explicit plain type pulls it back to plain"
escape hatch either (v i64 = a[i] must also be consumed explicitly).
1. READ — unwrap with a match
A value match yields the value directly, or a statement match assigns inside the ok arm.
-> _ is the discard arm; in a value context it yields the element type's zero value
(i64 → 0, str → '', byte → 0, container → empty container).
#{fmt-skip}
; value match
x = state[5]: {
ok -> it
-> _
}
; statement match
state[5]: {
ok -> x = it
-> _
}
2. WRITE — place match
it in the ok arm is the element place (readable and writable), not a value copy.
#{fmt-skip}
state[5]: {
ok -> it = x
-> _
}
map exception:
m['k'] = 1needs no unwrapping — a map subscript is a key and the write is a put/insert (missing key inserts, existing key overwrites), so there is no out-of-bounds write to guard. But a map readm['k']still returns?Vand must be unwrapped.m[k] ?= vis a compile error (the?Vfromgetis only a copy; writing it back does not reach the map) — writem[k] = vdirectly.
3. The ?= shorthand — propagate the error
x ?= state[5] (read) and state[5] ?= x (write) are the shorthands for the two match forms
above; on failure the whole option is thrown upward. It is legal under either of two conditions:
- the enclosing function has a
?Tresult param — on failure the option is written there and the function returns early; - the statement is inside the body of a statement-level block match
{ ... }: { ... }— the nearest block match captures it, the whole body stops and the arms run once; no?Tresult param required.
Outside both it is a compile error. So the standard way to keep the shorthand in a function whose return type is not option is to wrap the body in a block match:
#{fmt-skip}
handle = (x i64) {
{
y ?= x + 1
print(1)
}: {
err -> _
-> _
}
}
Nested reads: an inner match rebinds it, so use a named binding ok(it1) -> it1 there.
Since 2026-11-06 the exact shape with a value match directly on the right of it = ... does
write back to the container: lowering hoists that value match into a synthetic temporary before
rebinding it (regression: the place-write-value-match section of tests/match.no).
Prefer the direct form: bind the value match straight to the name that will actually use it,
instead of routing it through a one-shot temporary and copying it back.
#{fmt-skip}
; Preferred: bind the target variable directly
at = argt[i]: {
ok -> it
-> _
}
s.farg.push(at)
; Avoid: a one-shot temporary used exactly once
arg-at = argt[i]: { ok -> it -> _ }
at = arg-at
s.farg.push(at)
Hoisting the value match into your own named temporary and then writing it = arg-at inside the
arm is not equivalent to the direct form, and must not be treated as an interchangeable
formatting choice:
- Evaluation timing. The direct form evaluates the read only when the outer
okarm is taken; hoisted outside, the read also happens onnil/err/ catch-all paths. - Binding and ownership.
itexists only inside match arms and denotes the element place, so the copy-back must stay inside the arm. A named temporary is a new binding with its own move, ownership and scope-exit destruction. - Inner binding form. A nested inner match needs a named binding (
ok(it1) -> it1); the statement-levelok -> itcannot be moved back into a nested position unchanged.
Only hoist when the same value is genuinely reused in several places, and give it a meaningful name that reflects what the value is. For a single use, bind directly.
Clearing the remaining one-shot temporaries with ?= and block match
The rule above is "bind directly when the value is used once". One step further: safe indexing
returns ?T, so when the function already has an option-typed result parameter (?T), no value
match is needed at all — ?= propagates in a single line:
#{fmt-skip}
; Preferred: the function has a ?T result param — no temporary, no match
at ?= argt[i]
s.farg.push(at)
When the function's result is not option (the common case: a helper with no return value, or one that just writes back into a container), wrap the statement in this try-like form:
#{fmt-skip}
handle = (x i64) {
{
y ?= x + 1
print(y)
}: {
err -> _
-> _
}
print('continues afterwards')
}
⚠️ This is not a new try syntax — the language has no try keyword. It is an ordinary
statement-level block match ({ ... }: { ... }) whose subject is a three-state ?T; ?=
throws its failure up to the nearest block match (see Error Propagation with ?= and
Block Match — the Third Match Subject Form). On failure the rest of the block body — including
later loop iterations — is skipped, the arms run once, and execution continues after the block;
it is not a function return. err -> _ discards the error, -> _ is the catch-all that discards
the success result; to handle the error, write err -> print(it.msg()).
⚠️ State the semantic difference precisely (measured, not assumed): on failure ?= still
writes the zero value to the target — i64 gets 0, str gets '', out[1] gets 0 — exactly
as the old catch-all -> _ did. The only real difference is that ?= skips the statements after
it in the block body (an early return in a ?T function, or a jump to the arms inside a block
match).
So: when the block body holds only that one statement, the old and new forms behave identically
(which is exactly the shape this repository rewrote in bulk). They diverge only when other
statements follow ?= in the same block body (loop counters, later assignments) — do not replace
those mechanically.
One more trap: the arms also run once on success, with the catch-all receiving the success
value (-> print('x') does print). Keep arms to discards or pure handling; the err -> _ / -> _
shape has no side effects.
This gives the order of work for clearing one-shot temporaries:
- Value used exactly once → bind it straight to the final target name (see above).
- Function has a
?Tresult param →TARGET ?= EXPR, no temporary. - Otherwise → put the statement inside a block match and write
TARGET ?= EXPR; a place-match write-back (SUBJ: { ok -> it = temp -> _ }) likewise collapses toSUBJ ?= EXPR. - Keep a named temporary only when the same value is genuinely reused in several places, and give it a meaningful name.
Two boundaries: ?= is a compile error (l86f91me) when there is neither a ?T result param nor
an enclosing block match; a typed declaration cannot be the LHS of ?= (ai i64 ?= a[i] is
a syntax error, f5ikdmuc). When an explicit byte → i64 promotion is needed, use a typed
direct value match instead:
#{fmt-skip}
ai i64 = a[i]: {
ok -> it
-> _
}
Scope: applies to
arr/vec/slice/ fixed-length arrays /str/txtalike; only a direct variable base (v[i]) is rewritten into the safe form.receiver.field[i]goes through the original bounds-check path. Comparison operands compare the payload (a[i] == 5), not the whole{tag, payload}.
⛔ Hard error: using a
?Tas a plain value (crc = crc ^ s[i],f(s[i]),v i64 = a[i]) blocksno build; the compiler suggests a match or?=(wrap in a block match when the function has no?Tresult param). The corpus was migrated in one pass byno fmt --fix=index-out; that tool was retired with D11.
Core guarantee: an out-of-range index never crashes silently — it is either handled by a match or propagated by
?=.
An index read used as a comparison operand follows safe indexing too:
a[i] == 5compares the payload (the same rule as?T == 5), not the whole{tag, payload}. Out of range the read yieldsnil, whose payload is the element type's zero value — so out of rangea[i] == 0is true anda[i] == 5is false, and no out-of-bounds memory is ever read.s[0] == 'a'works the same way: comparing a code point with a one-character string uses the implicitchar → strconversion.
Nested subscripts
a[i][j]: both levels are safe index reads. The outera[i]is bounds-checked and tested for presence first, and only then is[j]applied to it; if either level is out of range the whole expression isnil. Soa[oob][0] == 'c'is false rather than aborting.
Container comparison
[]T == []T(and[N]T == [N]T) is structural: lengths first, then element by element. When the element is itself a container ([][]i64) the comparison recurses; when it is astrit compares the contents, not the data pointer. Ordering comparisons (</<=/>/>=) are not defined for containers — only==/!=.
nilcan only be compared with an option:?T == nil/?T != nilare legal;T == nilfor a non-optionT(str,[]i64,[N]T,txt,i64, structs, …) is a compile error (q4nilcmp). This is not pedantry:str == nilis always false (it compares a%str-longstruct against 0), and[]i64 == nilfalls into the integer comparison path and emits invalid LLVM (icmp eq i64 %vec, i64) — both silently give the wrong answer. To express "may be absent", declare the type as?Tand compare that withnil.
Structs
Struct definitions and literals must both use the multi-line form, with each field on its own line. Fields are not separated by commas, and there is no trailing comma.
user {
name str
age i64
}
u = user {
name: 'Alice'
age: 30
}
u.name = 'Bob'
u.age = 25
print(u.name)
Structs Implementing Interfaces
A struct can implement one or more interfaces; the interface names follow the struct name, separated by commas.
; Implement a single interface
user json {
name str
age i64
}
; Implement multiple interfaces
file enter, leave {
path str
fd i64
}
Cross-Module Interface Implementation
When implementing an interface defined in another module, the interface name must carry the module prefix (ShortName.). See Cross-Module Call Prefix.
#{fmt-skip}
; ❌ Wrong: db, rows and stmt are interfaces defined by the sql module; the prefix cannot be omitted
db-mysql db {
fd i64
}
; ✅ Correct: use sql.db, sql.rows, sql.stmt
db-mysql sql.db {
fd i64
}
rows-mysql sql.rows {
fd i64
}
stmt-mysql sql.stmt {
fd i64
}
Built-in interfaces (
enter,leave) and interfaces defined in the same file need no module prefix.
Methods
Methods are defined on types and use . to reference the receiver.
Syntax
#{fmt-skip}
type.method-name = (params) (results) {
; . is the receiver
}
Rules
- Method names use the
type.methodformat; type must be a defined type - The receiver does not need to be declared as an explicit parameter; it is referenced via
.within the method body - Calls use the
receiver.method(args)syntax - Return values are placed in the second set of parentheses, consistent with ordinary functions
- Syntactic-sugar desugaring:
type.method = (inputs) (rest-outputs...) {}is equivalent to the ordinary functionmethod = (inputs) (self type, rest-outputs...) {}. Wheninstance.method(args)is called, the instance binds to the first parameter of the output list,self; inside the body.refers to the output slotself; writes toselfmodify the original instance directly. The remaining outputs in the list (everything exceptself) may optionally be received by destructuring, or simply dropped.
Example
#{fmt-skip}
; str method
str.to-upper = () (out str) {
n = .len-bytes()
b []byte = with-cap-len(n, n)
i <- [0..n): {
c ?byte = .byte(i)
v = c: {
ok -> it
nil -> 0
err -> 0
}
{
v >= 97 && v <= 122 -> b[i] = v - 32
-> b[i] = v
}
}
out = b.to-bytes-str()
}
; char method
char.is-digit = () (result bool) {
result = false
. >= 48 && . <= 57 -> result = true
}
; struct method
user {
name str
age i64
}
user.greet = () {
print('Hello, ' - .name)
}
; Call
s = 'hello'
u = s.to-upper() ; receiver.method()
c char = 5
d = c.is-digit() ; receiver.method()
u = user{
name: 'Alice'
age: 30
}
u.greet()
Interfaces
; Define an interface
json {
to-json()
}
; Default interface implementation
json.to-json = () {
}
noml {
to-noml()
}
serialize {
json
noml
}
serialize = json | noml
serialize = json & noml
; Interface implementation
user json {
name str
age i64
}
; Override + call parent implementation
user.to-json = () {
; Parent implementation
..to-json()
}
user.other = () {
; Current implementation
.to-json()
; Parent implementation
..to-json()
}
Interface Merging
An interface can merge the methods of other interfaces: write the parent interface names inside the interface body, one per line (module prefixes allowed), with no commas and no parentheses:
json {
to-json()
}
noml {
to-noml()
}
serialize {
json
noml
}
; Transitive merge: roundtrip gets every method of serialize, json and noml
roundtrip {
serialize
roundtrip-json()
}
Rules:
- Recursive merge: merging is transitive —
roundtripmergesserialize, andserializemergesjson/noml, soroundtripowns the methods of all three interfaces. - One per line, no commas:
serialize { json, noml }has the same shape as a C-style enum (color { red, green }), so it is still parsed as an enum, not as interface merging. - Same-name methods: identical signatures (same parameter and result types; the receiver name may differ) ⇒ deduplicated into one; different signatures ⇒ a diagnostic is reported.
- Implementing side: a type only declares the interfaces it implements (leading list,
user serialize { name str }); methods of parent interfaces count as implemented too, and signature checking reaches through parents. no fmt: the older "leading list" spelling (serialize json, noml { }) still parses, butno fmtrewrites it into the in-body form andno vetemits a WARNING about it.
Diagnostics (shared by no vet and the editor, TraceID ifmrg001):
| Case | Message |
|---|---|
| Parent interface not declared | interface 'serialize': unknown parent interface 'noml' |
| Merge cycle | interface 'a': merge cycle a -> b -> a |
| Same name, different signature | interface 'serialize': merged method 'to-str' has conflicting signatures ('json.to-str', 'noml.to-str') |
Spellings such as
serialize = json \| nomlare union type syntax and have nothing to do with interface merging; interface merging only affects which methods a type must provide.
Special Interfaces
file {
enter
leave
}
Enums
; red=0, green=1, blue=2
color {
red,
green,
blue,
}
; In ordinary methods, a, b, c are actually defined as a=0, b=1, c=2... This is inconsistent with other languages.
; So normally you cannot use commas to define multiple variables
; This is a special enum; it can have types, commas, and aliases
enum-name {
a t,
b u,
c v,
}
; Note: this is an ordinary struct; multiple fields have no commas
struct-name {
a t
b u
c v
}
Tagged Enums (with Payload)
A variant may carry named payload fields, written in the parenthesized form variant(field type); a payload-less variant is written as a bare name.
A single variant may carry multiple fields (a multi-field payload is represented on the heap as a synthesized struct):
#{fmt-skip}
; Tagged enum: variants may carry a payload; tags are 0,1,2... in declaration order
result {
ok(v t), ; has a value: payload field v of type t
nil, ; null value: no payload
err(e str), ; error: payload field e of type str (user-defined enum — unrelated to the builtin option)
}
shape {
circle(r f64), ; single field
rect(w f64, h f64), ; multiple fields
dot, ; no payload
}
In a match, the variant name is used as the arm pattern; a variant with a payload can destructure-bind its payload (multiple fields bind positionally, one to one):
#{fmt-skip}
r result = ok(42)
r: {
ok(v) -> print(v) ; bind ok's payload to v
nil -> print('empty')
err(e) -> print(e)
}
s shape = rect(3.0, 4.0)
s: {
circle(r) -> print(r) ; bind the single field
rect(w, h) -> print(w * h) ; bind multiple fields positionally
dot -> print('dot')
}
A variant can also be constructed directly in expression position, as a function argument or as another variant's payload:
#{fmt-skip}
x f64 = perimeter(rect(3.0, 4.0)) ; constructed inline and passed as an argument
o outer = wrap(a(5)) ; an enum payload that is itself another enum
Namespaces and bare-name resolution: internally the compiler registers variants under a fully-qualified name (
module.Enum.variant, e.g.option.option.ok,some-mod.my-result.ok), so same-named variants of different enums never collide. Users only write the bare name (ok,rect), and the compiler fills in the full name from the static type of the variable being matched or constructed. Two enums can therefore both have anokvariant with different tag orders and still be told apart correctly.The older space-separated form
ok tmay also be written; it is equivalent took(v t)(the field name omitted). A tagged enum's variant names are registered in the compiler's enum-variant table for matching and exhaustiveness checking.
Built-in Tagged Enums (#{buildin})
Prefixing a tagged enum with #{buildin} marks it as a built-in enum: its variants (names, order,
payload types) are used for matching and exhaustiveness checking, but the underlying representation and
construction come from the runtime/builtin — no user-visible struct/union is generated. ?t option
is declared this way:
; src/std/global.no
#{buildin}
option {
ok(v t),
nil,
err(e err.err),
}
option's tags are 0=ok, 1=nil, 2=err in that order, matching the built-in option representation.
Users do not need to declare option themselves; the #{buildin} here makes the built-in option's
variant source single-sourced (see "Nullable Types (option)").
Enum Annotations and Memory Layout
Enums accept #{...} annotations, subject to the same placement rule as every other target (see
Annotation Placement): above (on its own line) or trailing (on the
target's line, after it). Writing the annotation on the same line in front of the target is a
compile error.
There are two places an enum annotation may go:
- Above the whole enum — applies to the enum definition itself (e.g.
#{buildin},#{inline=true}); - On an individual member, above or trailing — applies to that member (both the values of a C-style enum and the variants of a tagged enum).
; 1. whole enum: own line above
#{inline=true}
box {
full(v i64) #{doc='has value'},
empty #{doc='empty'},
}
; 2. one variant: own line above
; 2. one variant: trailing on the same line
; `#{inline=false}` states the default explicitly
#{inline=false}
plain {
a,
b,
}
color {
red #{deprecated},
green #{deprecated},
blue,
}
; values of a C-style enum support the same two spellings
Annotations never change the enum's structure: variant names, declaration order (tags), payload fields and their types are all unaffected, as are matching and exhaustiveness checking. An annotation is metadata attached to the definition.
Memory Layout (the Stack Form)
A tagged enum is stored using the inline stack form — a small by-value object, with the payload not scattered on the heap:
%tenum_<name> = type { iN tag, [K x i64] payload } ; the usual shape
%tenum_<name> = type { iN tag } ; every variant is payload-less
tag's width follows the variant count and is a completeu8/u16/u32/u64:<= 256→i8,<= 65536→i16,< 2^32→i32, otherwisei64. Discriminants are0, 1, 2...in declaration order.payloadis a union shared by every variant (each variant writes its fields into the same storage);Kis the number of 8-byte words the widest variant needs — not the sum of all variants — and is at least 1.- Fields inside a variant are placed with natural alignment, so a field offset is not
necessarily a multiple of 8 (
(a char, b char, c char)puts them at 0 / 4 / 8). Reads and writes address them by byte offset (bitcasttoi8*, thengetelementptr), so the offset written and the offset read come from the same layout table.
The size is 8 + 8K (the tag is padded to a whole word by the array's 8-byte alignment); only an
enum with no payloads anywhere is tag-sized:
| Case | K | Size |
|---|---|---|
| Every variant is payload-less | — (no payload field) | 1 byte ({ i8 }; 2 / 4 / 8 once past 256 variants) |
Widest variant is i64 / i32 / f64 / a pointer | 1 | 16 bytes |
Widest variant is char (4 bytes) / bool (1 byte) | 1 | 16 bytes |
Widest variant is (a char, b char, c char) (12 bytes) | 2 | 24 bytes |
Widest variant is str / vec / []T (16-byte descriptor) | 2 | 24 bytes |
Widest variant is a struct T | ceil(sizeof(T) / 8) | 8 + 8K |
In other words the layout starts at 16 bytes by default and only grows when some variant's
payload really is wider — "fixed 16 bytes by default" and "size varies per variant" are two sides of
the same rule. Small payloads such as char / bool only affect alignment, they do not enlarge the
union: its width is the single widest variant, never the sum.
The single source of truth is
src/mir/layout.go:tagWidthpicksiN,enumVariantLayoutcomputes each variant's field offsets and own size, andenumUnionLayouttakes the maximum to getK.hir2mir.fillEnumLayoutrecords those numbers inTaggedEnumInfowhen the table is built and the emitter only reads them — the two cannot disagree.
The Three Spellings of #{inline}
inline is a boolean annotation with three spellings, and its value is honoured:
| Spelling | Meaning |
|---|---|
#{inline} | shorthand for inline=true (a bare key is true) |
#{inline=true} | explicitly true (#{inline=1} also works) |
#{inline=false} | explicitly false (#{inline=0} also works) |
A non-boolean value is a compile error (#{inline=foo}, #{inline='true'}) rather than a
silent "true": ValidateFieldTags (TraceID fieldtag1) reports it for both struct fields and
enum definitions.
Writing #{inline=true} above the whole enum is the explicit marker for the stack layout,
meaning "this enum uses the stack form"; the compiler records it in TaggedEnumInfo.Inline.
The default layout already is the stack form, so the annotation — and whether you write true
or false — produces a byte-identical layout. The marker exists to make the intent explicit and to
act as a stable switch should the default ever change.
#{inline}above the whole enum means "this enum uses the stack layout".#{inline}on an individual variant currently does not change the layout — it is kept only as that variant's metadata. This differs from a struct field; see the next section.
#{inline} on Struct Fields
Unlike enums, #{inline} on a struct field really does have an effect: it decides whether the
field is stored by value inside its host (%pt) or as a pointer (ptr).
#{fmt-skip}
pt {
x i64
y i64
}
holder {
inl pt #{inline=true} ; by value: the struct lives inside holder
out pt #{inline=false} ; pointer: the field holds a pointer to pt
bare pt #{inline} ; shorthand for inline=true
}
| Spelling | Field layout |
|---|---|
#{inline} / #{inline=true} / #{inline=1} | by value, inlined in the host (%pt) |
#{inline=false} / #{inline=0} | pointer (ptr) |
| absent | depends on NOLANG_FIELD_PTR (see below) |
#{inline=foo} / #{inline='true'} | compile error — not a boolean |
The default when there is no annotation is decided by NOLANG_FIELD_PTR. mir.FieldPtrLayout
is os.Getenv("NOLANG_FIELD_PTR") != "" and is off by default; turning it on enables the
"Phase 1 layout flip", which changes a struct field's default from by-value to pointer.
| Field | Flag off (default) | Flag on (NOLANG_FIELD_PTR=1) |
|---|---|---|
| no annotation | %pt (by value) | ptr (pointer) |
#{inline} / #{inline=true} | %pt | %pt |
#{inline=false} | ptr | ptr |
In other words the annotation is always honoured, regardless of that environment variable — the
flag only changes the default when no annotation is written. So under the default configuration
#{inline=false} is "actively ask for a pointer" while #{inline=true} merely restates the layout
that already applies; once the flip is enabled their roles swap.
The checker enforces two rules (TraceID fieldtag1, ValidateFieldTags):
-
Struct-typed fields only. A scalar /
str/vec/ array / slice / map / option / pointer field is always stored inline, so#{inline}on one asserts nothing and is rejected;#{inline=false}is rejected too, since it would claim the opposite of what the compiler does. -
No by-value cycles. An inlined struct is stored inside its host, so
a { #{inline} b b }together withb { #{inline} a a }has no finite size and must be rejected (direct self-reference is the degenerate case). Only=truecreates a by-value edge, so mutually recursive structs are legal when both sides write#{inline=false}.⚠️ "No annotation" does not mean "not inline". Under the default configuration an unannotated field is by-value, so
a { x b }together withb { y a }(neither annotated) is not legal by default — it fails withopt: error: identified structure type 'b' is recursive. That error is currently not reported by the checker; it only surfaces at the LLVM verification stage. To write recursive types under the default configuration you must write#{inline=false}explicitly. Only withNOLANG_FIELD_PTR=1does "omit it" mean "pointer".
Recursive types: why inline=false exists
A struct that inlines itself has infinite size, so a self-reference cannot be stored by value.
Under the default configuration (NOLANG_FIELD_PTR off) a struct field is by value, so asking for
a pointer explicitly is the only way to write a type that refers to itself:
#{fmt-skip}
node {
v i64
next node #{inline=false} ; pointer: this is what gives node a finite size
}
n node
n.v = 1
n.next.v = 2
print(n.next.v) ; 2
Drop the #{inline=false} above (or write #{inline=true}) and LLVM rejects the type outright:
identified structure type 'node' is recursive.
A pointer field owns its pointee, and the compiler manages its lifetime:
- the pointee is allocated lazily, on first write (
@__nolang_get_<T>, which mallocs and zeroes), not eagerly when the host is constructed; - when the host leaves its scope,
@__nolang_drop_<T>null-checks and frees it; - a by-value copy (
b = a, passing by value, writing into a container) deep-copies the pointee, so two hosts never share one allocation and it is never freed twice.
End-to-end coverage lives in tests/field-inline-annotation.no.
The annotation is always honoured;
NOLANG_FIELD_PTRonly changes the default when there is no annotation.mir.FieldPtrLayoutisos.Getenv("NOLANG_FIELD_PTR") != ""and is off by default. It decides the layout of an unannotated struct-typed field only; a field carrying#{inline=true}or#{inline=false}follows its annotation regardless of the flag.
Field Flag off (default) Flag on ( NOLANG_FIELD_PTR=1)no annotation %pt(by value)ptr(pointer)#{inline}/#{inline=true}%pt%pt#{inline=false}ptrptr
Enum Value References
Rule: Enum values must be referenced using the qualified EnumType.value form; bare values cannot be used directly.
This prevents naming conflicts and also prevents external packages from using the concrete values directly.
#{fmt-skip}
; ❌ Wrong: using a bare value directly
kind = null
yes = e.is(io)
; ✅ Correct: using the qualified form
kind = json-kind.null
yes = e.is(code.io)
Enum types can be used as struct field types, function parameter types, and return value types. Both inside and outside the module that defines an enum, enum values should be referenced using the
EnumType.valueform.
enter/leave
Types that implement the enter / leave interfaces are automatically called when the scope is entered and left:
file enter, leave {
path str
}
file.enter = () {
.open()
}
file.leave = () {
.close()
}
read-file = () {
; Automatically f.enter()
f = file {
path: 'data.txt'
}
; Use f
; Automatically f.leave()
read(f)
}
Nullable Types (option)
Adding ? before a type indicates a nullable type:
A nullable type variable can legitimately hold a null value or an error value; the compiler will perform the corresponding null checks.
?t is a built-in tagged enum whose variants are defined in src/std/global.no:
#{fmt-skip}
#{buildin}
option {
ok(v t), ; tag 0: has a value
nil, ; tag 1: null value
err(e err.err), ; tag 2: error (the std structured error err.err)
}
The err variant's payload is not a str — it is the err struct from std/err
(code / msg / file / line / col). err('msg', code) is the alias constructor for
err.new('msg', code) — the error code is a required argument (parameter default values no
longer exist), so pass code.unknown explicitly when there is no specific code. The position
fields are injected by the compiler at the err(...) construction site. A one-argument
err('msg') is not this alias — it is the option's err variant constructor (see below).
#{buildin} means the underlying representation and construction are provided by the built-in runtime (the
%option_<key> type, and the ok(...)/nil/err(...) constructors); the option enum declaration exists only for
matching and exhaustiveness checking. See "Tagged Enums" and "Built-in Tagged Enums".
Memory Layout
?T is typed by its payload: every payload type gets its own LLVM type, named after the
payload type through an injective mapping, so "same payload type ⇒ same layout" — ?i32 / ?i64 /
?bool share one type:
%option_<key> = type { i8 tag, [K x i64] payload } ; inline
%option_<key> = type { i8 tag, [1 x i64] payload } ; boxed (the word holds an i8*)
- The tag is always one byte: an option has three variants (
0=ok,1=nil,2=err), which falls in the "<= 256variants →i8" bracket. The enum tag-width rule is described in "Memory Layout (the Stack Form)". - After the tag comes alignment padding (a 1-byte tag pads out to 7 bytes), and only then the payload.
Kfollows the actual size ofT, rounded up to a multiple of 8 — it is not a fixed slot:?i64is 16 bytes (8 + 8),?stris 24 (8 + 16),?txtis 264 (8 + 256).- A payload above the threshold is heap-boxed instead: the payload field holds a single 8-byte pointer and the whole option is 16 bytes.
The option-payload-inline-threshold counts the whole option struct in bytes (default 264 = 1-byte
tag + 7 bytes padding + 256-byte payload, minimum 16): a payload is inline iff
8 + roundUp8(sizeof(T)) <= threshold. So by default "a payload of at most 256 bytes stays inline",
and ?txt (a 256-byte inline buffer) lands exactly on that boundary.
T of ?T | Inline? | %option_<key> size |
|---|---|---|
i64 / i32 / bool / char / f64 / a pointer | yes | 16 bytes |
str / []T / vec (16-byte descriptor) | yes | 24 bytes |
txt (256-byte inline buffer) | yes | 264 bytes (exactly at the default cap) |
| a struct / array larger than 256 bytes | no | 16 bytes (payload on the heap) |
a self-referential payload (node { next ?node }) | no | 16 bytes (only a pointer can break the cycle) |
The
errpayload is always boxed: theerrvariant stores a pointer to std'serr.errstruct and never competes for the payload budget. So a?stris a 24-byte inline string in itsokstate and the same 24-byte struct in itserrstate — the payload word just holds the error object's pointer. That is what lets one?Ttype carry two very different variant payloads without extra space.
Inlining and boxing change WHERE the payload lives, never its ownership. Both paths give a struct payload its own heap whenever a copy is made (pointer fields recurse, inline-owned
str/[]Tbuffers are cloned), so?Tkeeps value semantics regardless of the layout: the same program must produce identical output under anyoption-payload-inline-threshold. A difference across thresholds is a compiler bug, not a feature. (A pure transfer does not clone — the option takes over the source's heap.)
The single source of truth is
src/mir/layout.go:optLayoutOfPayloaddecidesKand whether to box, andoptionPayloadOffset(8) is the payload's fixed byte offset.hir2mirfolds module-level?Tconstants and codegen emits the reads/writes — both ask the same function.
Cross-payload propagation in ?=
?= first copies the option at the root of the right-hand chain into the option result parameter
so that nil / err propagate straight out; the success path is then overwritten by the ok arm:
pick = (p pool) (out ?i64) {
out ?= p.nodes[0].kind ; root is ?node, the result slot is ?i64
}
The two payload types may therefore differ, and that is a real ABI boundary (?node is 112
bytes, ?i64 is 16), not two spellings of one layout. Only two things survive the type change:
tag— byte 0 of every option;err— theerrvariant belongs to the option, not to its payload, and it is always boxed, so both sides hold an 8-byte box pointer in word 0 of the payload area (the box is copied, not shared, because both options are dropped).
The ok payload is not carried across types: the destination payload is zeroed first and a
success value is always rewritten by the ok arm. The code lives in
@__nolang_opt_prop_<dst>__<src> (optPropagateState in src/mir/codegen.go) and only appears
for this compiler-generated propagation assignment.
#{fmt-skip}
o ?i64
o = nil ; set to null
o = 42 ; set to a value
o = err('msg') ; set to an error
nullableValue ?[]str
nullableString ?str
; Modify a nullable type
nullableString = 'test'
; Set an error
nullableString = err('some error')
; Can be checked via match
x: {
err -> log(it.msg()) ; it is err.err; the message needs .msg()
nil -> log('nil')
->
do-right-thing(it)
}
; Force unwrap
; Cancel implementation
;!x.say()
Style Guide: Use ?t option Instead of (val, ok)
When a function may fail or return a null value, prefer the ?t option type over the (val t, ok bool) dual-return-value pattern.
?t is a tagged enum with three states: ok (has a value), nil (null value), and err (error). Normal values are bound implicitly; use nil when an operation simply cannot find a value, and use err(...) when an operation encounters an actual error.
#{fmt-skip}
; ❌ Not recommended: dual-return-value pattern
stack.pop = () (val i64, ok bool) {
.n == 0 -> return
val = .data[.n]
ok = true
}
; ✅ Recommended: option type (nil for empty, err for error)
stack.pop = () (val ?i64) {
.n == 0 -> {
val = nil
return
}
val = .data[.n]
}
; ✅ Return an error
file.read = () (data ?str) {
.fd < 0 -> {
data = err('file not open')
return
}
; ... read data
data = buf
}
Use match to unwrap an option:
#{fmt-skip}
val = s.pop()
val: {
nil -> print('empty')
err -> print(it.msg()) ; it = structured error err.err
-> print(it) ; it = popped value
}
Applicable scenarios:
pop/peekand other container operations that may be empty →?t(nil= empty)read-line/read-byteand other I/O operations →?str/?i64(nil= EOF,err= error)lookup/getand other lookup operations →?t(nil= not found)parse/from-strand other parsing operations →?t(nil= empty,err= invalid input)accept/dialand other network connections →?conn(nil= no connection,err= error)
nil vs err: Use nil when absence is a normal/expected result (empty stack, key does not exist, EOF); use err('msg') when absence represents an actual error state (I/O failure, invalid input, connection refused).
Exception: When a function needs to return multiple independent values (such as (name str, value str, ok bool)), the multiple-return-value pattern may be retained.
Integer Overflow (the #{overflow} Annotation)
Nolang has no panic — any operation that could overflow must be handled with an option or an explicit annotation,
and an integer overflow is never thrown as a runtime exception. The overflow behaviour of the following integer
arithmetic operations is controlled by the #{overflow = ...} annotation:
- Signed and unsigned
+-*: applies whenever the operands are integers (includingintliterals); - Signed
/: onlyINT_MIN / -1can overflow (unsigned divisiona/b ≤ anever overflows and is not affected).
The
+ - *of unsigned integers (u8/u16/u32/u64/u128) follow exactly the same rules as signed integers: unannotated, they also default to returningoption<int>, with overflow →err. This is a deliberate design choice — "if it cannot be proven at compile time that overflow is impossible, treat it by the same rules".
| Mode | Annotation | On overflow | Result type | Suitable for |
|---|---|---|---|---|
| Default (unannotated) | — | overflow → err, otherwise → ok(value) | ?int | operations that need overflow detection |
| Wrap | #{overflow = wrap} | silent two's-complement wrap | ?int | hashing, cryptography, counters — semantics that tolerate wrapping |
| Clamp to zero | #{overflow = clamp0} | overflow (above or below) becomes 0 | ?int | semantics where a difference must not go negative (e.g. a remaining amount) |
| Clamp to minimum | #{overflow = min} | overflow clamps to the type's minimum | ?int | counter lower bounds, index protection |
| Clamp to maximum | #{overflow = max} | overflow clamps to the type's maximum | ?int | capacity limits, saturating accumulation |
| Saturate | #{overflow = saturate} | above → maximum, below → minimum | ?int | signal/colour saturation, etc. |
⚠️ Annotations do not unwrap (D1):
#{overflow = …}/#{index-out = …}only decide "what value to take on failure" — the result type is always?T. To pull the result back to plain, write an explicit plain type (w i64 = a - b #{overflow=wrap}) or consume it with a match /?=. Both affect "what happens on overflow" identically; only the type differs.
All modes also support a type-prefixed form that pins the saturation bound to a specific narrow type, e.g.
#{overflow = u8-max}, #{overflow = i8-min}, #{overflow = u16-saturate}.
Annotation granularity:
#{overflow = ...}is a line annotation — it applies only to the statement immediately following it. That is the only fully supported form: both the runtime semantics (codegen) and theovfhndldhard error honour it.⚠️ An annotation above a function definition no longer covers the function body. It only makes the
ovf-int-defaultlint skip the whole function (so the report looks clean), while unannotated operations inside the body still raise theovfhndldhard error — measured to behave exactly like writing no annotation at all. Annotate statement by statement;no fmt --fix=overflowemits exactly this per-statement form.
LSP quick fix: the editor (nolang-lsp) reports unannotated integer arithmetic as a hint (
nolang-overflow, trace idovfhndld) and offers five quickfixes — Add#{overflow = wrap}/clamp0/min/max/saturate— which insert the corresponding annotation above the statement holding the operation, using that line's indentation, pinning the overflow behaviour (the result is still?T, so it must be consumed separately with a match /?=/ an explicit plain type). The command-line equivalent isno fmt --fix=overflow.
Default: returns option<int>
Unannotated, the result type of a - b is option<int>. The receiver must be a ?T (nullable type), and a match
must destructure the three branches err / nil / ok:
#{fmt-skip}
x i64 = 100
d ?i64 = x - 1 ; default: returns option<i64>
d: {
err -> print(-1) ; overflow (e.g. x - 2^63 underflow)
nil -> print(0)
-> print(1) ; ok(value): the normal result
}
%option'sdatafield is ani64; a narrow type's result (i8/i16/i32) is sign-extended (sext) before being stored, and truncated back to its original width on unwrap.
wrap: silent wrapping
#{fmt-skip}
#{overflow = wrap}
sub-wrap = (a i64, b i64) (r i64) {
r = a - b ; silently wraps (two's complement) on overflow; returns plain i64
}
Statement level:
#{fmt-skip}
x i64 = -9223372036854775807
#{overflow = wrap}
w i64 = x - 2 ; underflow wraps → 9223372036854775807
print(w)
clamp0: overflow becomes zero
#{fmt-skip}
x i64 = -9223372036854775807
#{overflow = clamp0}
c i64 = x - 2 ; underflow → 0 (overflow also becomes 0)
print(c)
min / max / saturate: saturating clamps
These three modes "squeeze" an overflowing result into the type's representable range instead of wrapping or zeroing:
#{fmt-skip}
#{overflow = min}
floor = (a i64, b i64) (r i64) {
r = a - b ; underflow → i64 minimum; overflow → i64 maximum
}
#{overflow = max}
cap = (a i64, b i64) (r i64) {
r = a * b ; overflow → i64 maximum; underflow → i64 minimum
}
#{overflow = saturate}
sat = (a i64, b i64) (r i64) {
r = a + b ; overflow → max, underflow → min
}
The type-prefixed form pins the saturation bound for a narrow type precisely (a function-level annotation applies to every matching operation in the body):
#{fmt-skip}
#{overflow = u8-max}
inc = (x u8) (r u8) {
r = x + 1 ; x = 255 overflows → 255 (instead of wrapping to 0)
}
#{overflow = i8-min}
dec = (x i8) (r i8) {
r = x - 1 ; x = -128 underflows → -128 (instead of wrapping to 127)
}
Compiler Enforcement
An unannotated integer operation defaults to returning option<int>, so an unhandled one is caught. What counts as "handled" is judged in parallel — satisfying any of the following silences the error:
?=propagation (requires the function to have an option result param);- In-place capture: binding with
=to a new variable (the variable is inferred as?T), or an explicit?Tdeclaration (e.g.x ?i64 = a + b); _ = exprexplicit discard (the expression is still evaluated on the safe path; the error is ignored too);- An annotation —
#{overflow = wrap}/clamp0/min/max/saturate(or a type-prefixed form such asu8-max,i8-min) — declaring that wrapping / saturating semantics are accepted.
Only when none of the four is present does the compiler report an error (ovfhndld).
Pure arithmetic is not a fallible source:
+ - * <<and negation do not trigger capture (otherwise every arithmetic expression would become an option and the standard library would explode). Sod = x - 1(no/, no%, no safe index, no option operand) still errors — add an annotation, or writed ?i64 = x - 1.Existing variables keep their type: capture only applies to a target first declared by that statement. If the variable already exists (e.g.
d = 0followed byd = x - 1, orv i64 = 0followed byv = arr[i]), the compiler will not turn it into an option in place; it reports an error asking you to choose the semantics explicitly.
Two hard errors are unaffected by the exemptions above:
- An unannotated integer operation assigned to an explicitly plain
intvariable is a compile error (cannot assign ?i64 value to i64 variable), forcing you to add#{overflow = wrap}/#{overflow = clamp0}/#{overflow = min}/#{overflow = max}/#{overflow = saturate}(or a type-prefixed form), or to use a named?T. - An unannotated integer operation assigned to an already-existing variable whose type is not an option (
d = x - 1afterd = 0) is a compile error that suggests adding the annotation or declaring?i64, so that invalid IR is never produced.
The remaining rules:
- Unsigned integer operations (
u8/u16/u32/u64/u128) are subject to the same mechanism: unannotated,+ - *default to returningoption<int>with overflow →err, matching the signed rules. i128operations do not support theoptionpath (the data field is onlyi64) and uniformly fall back to silent wrapping to preserve correctness.
Why is the default
optionrather thanwrap? Silent wrapping hides overflow bugs; returning anoptionby default forces the caller to handle overflow explicitly (or to annotatewrapand declare "I accept wrapping semantics"), turning "is overflow acceptable here?" into a visible design decision.
Division by zero
A 0 divisor for integer / and % is undefined behaviour (UB), and it is independent of overflow — no
annotation can exempt it:
- Decidable at compile time: the divisor is the literal constant 0 (
10 / 0,a % 0,x / 0) ⇒ a hard error (trace iddiv-by-zero).#{overflow = wrap}does not exempt it (that annotation governs overflow, not division by zero). - Known only at run time: the divisor comes from a variable or external input ⇒ the compiler inserts a zero check on both paths, and they differ only in how the danger is reported:
| Result type | Trigger | When the divisor is 0 |
|---|---|---|
?int (default) | statement unannotated | option tag = err, handled with ?= / match |
plain int | statement carries #{overflow = ...} | result is forced to 0, and a diagnostic is written to stderr: nolang: integer division or remainder by zero ... |
#{fmt-skip}
safe = (a i64, b i64) (r ?i64) {
r = a / b ; b == 0 → err (the caller must handle it)
}
#{overflow = wrap}
fast = (a i64, b i64) (r i64) {
r = a / b ; b == 0 → 0 plus one stderr diagnostic (no longer a raw sdiv / UB)
}
Why
#{overflow = wrap}cannot exempt the zero-divisor guard: it changes the result type (?int→ plainint), not the safety of the divisor. If it exempted the guard, the compiler would emit a rawsdivand the optimizer would be licensed to assume the divisor is never 0 (foldinga / 0into anything it likes), whileidivon x86-64 raises #DE outright — undefined behaviour and no diagnostic. The zero-divisor guard therefore exists independently of the annotation; the plain path writes a stderr line precisely because that path has no option for the caller to inspect.The same guard covers
sdiv (INT_MIN, -1)(the result is not representable, so it is UB in the same way, and it raises SIGFPE on x86-64). The option path folds it intoerr; the plain path yields the two's-complement wrap value (INT_MIN / -1 → INT_MIN,INT_MIN % -1 → 0).
Floating-point division is out of scope: IEEE-754 defines 1.0 / 0.0 as inf / nan, which is not UB.
Unannotated operations reported as errors (ovf-int-default)
no vet reports unannotated integer arithmetic as an error (trace id ovf-int-default), not a hint. The
reason: the default option<int> silently drifts in meaning the moment it is used as a plain integer, so the choice
must be explicit — add a #{overflow = ...} annotation, or handle the overflow with ?T plus ?= / match.
Automatic fix: no fmt --fix=overflow
Instead of hand-writing annotations in bulk, use no fmt's fix-class argument:
no fmt --fix=overflow -w src/std # fix a whole directory in place
no fmt --fix=overflow -d src/std/str.no # print the diff only, do not write
no fmt --fix=overflow src/std/str.no # print the fixed content
The fix is precise and non-polluting: it inserts #{overflow=wrap} only above the statement holding the
operation the lint actually reported — no other statement is touched, nothing is re-laid-out, existing annotations
are left alone, and re-running is idempotent (already-annotated statements never change). This differs from running
no fmt -w, which reformats the entire file.
Re-run
no vetafter fixing to confirm it reaches zero. In the overwhelming majority of cases one pass clears everything (src/stdwent from 4728 to 0). The one known exception: a report that lands on a match arm condition while the arm body holds no annotatable statement — a line annotation applies only to the statement immediately after it, so it cannot cover the arm's own condition arithmetic, andno vetreports it again. In that case hoist the operation into its own statement first, then annotate it.
Automatic fix: redundant type annotations
When a variable declaration's type annotation is exactly the same as the type inferred from the right-hand value, the annotation is redundant (reported by no vet as the hint tcpoxtfd, type annotation '<T>' can be omitted (inferred from value)).
- Removed by the default
no fmt:no fmt -wremoves redundant annotations while formatting (performing a normal reformat at the same time),no fmt -dpreviews including this change,-wwrites in place.
no fmt -w main.no # format and remove redundant annotations
no fmt -d main.no # print the diff only (including annotation removal)
no fmt -w src/ # a whole directory
--fix=redundant(surgical, touches only annotations): if you only want to remove redundant annotations without a full reformat (e.g. forsrc/std, sinceno fmtwould drop#{index-out}), use this fix class — it deletes only annotations and does not re-lay-out:
no fmt --fix=redundant -w src/std # remove redundant annotations across a directory in place (no re-layout)
no fmt --fix=redundant -d src/std/str.no # print the diff only, do not write
no fmt --fix=redundant src/std/str.no # print the fixed content
; before the fix
x str = ''
b i64 = 5
m [str]i64 = make-map()
; after the fix (annotation == inferred type, removed)
x = ''
b = 5
m = make-map()
Key rules:
- Removed only when the annotated type == the inferred type (e.g.
str/i64/[str]i64matching their inferred value). - Cases that are implicitly convertible but not equal are not removed — keeping the annotation preserves the conversion semantics. For example
c i16 = 5(5infers asi64, not equal toi16),p ?i64 = nil,last txt = ''(txtandstrconvert implicitly but are different types). - Forms without a type annotation (e.g.
d = 'x') are never reported in the first place, so they are left alone. - Fixed-size array annotations
[N]T/[?]Tare always kept (never treated as redundant).[N]is not a mere type hint — it is the switch that selects the representation: only with it written does the[...]literal on the right parse as a fixed-length stack array, and only with it written does the inference come out "equal to" the annotation (a circular argument the annotation itself creates). Re-parsing the line after deleting it yields a heap-backed growable slice[]T, changing the semantics:fixed [3]char = ["x", "y", "z"]ignores.push()(.len()stays 3), whereas the degradedfixed = ["x", "y", "z"]grows to.len()4. Slice annotations[]Tare not in this category — removing one still leaves a slice, so it is genuinely redundant. - Any value containing a hexadecimal literal always keeps its annotation (a conservative exception).
no vet's type inference uses an "infer asbyte" heuristic for0xNN, which differs from the compiler's actual semantics ("integer literals default toi64when unannotated"), soIP-TBL [64]byte = [0x3a, ...]ands []byte = [0x50, 0x4b]are misjudged as redundant; once removed, the literals degrade toi64elements and produce type errors likeexpected '[]byte', got '[]i64'. So whenever0x/0Xappears in the value expression text, neither the hint nor the removal happens. - Annotations on a field-access right-hand side are always kept (a conservative exception). In
i i64 = .posthe annotation is not a mere type hint: it is the switch that decides whether downstream integer arithmetic produces?T. Without iti + Nis no longer signed arithmetic and stops returning?i64, theokarm of the two-step unwrap never matches, and the loop counter never advances. Concretely, insrc/std/yaml.no'sparser.looks-like-map-entry, removingi64makesyaml.parse('abc')loop forever. Thereforex = .fieldandx = obj.field(including grouped or prefix-wrapped forms) are never reported bytcpoxtfdand are never removed by either the defaultno fmtor--fix=redundant.
Whether via the default no fmt or --fix=redundant, removal only deletes the reported type annotation and its leading whitespace, and re-running is idempotent (already-removed ones do not change again). After fixing, it is recommended to run no vet once more to confirm tcpoxtfd reaches zero.
Automated fix: consume the option (--fix=unwrap)
Under the unified rule, "an option is produced but never consumed" is the only diagnostic that should block a build (trace optunuse). To convert such bindings into their explicit consumption form in bulk, use --fix=unwrap:
no fmt --fix=unwrap -w src/std # fix a whole directory in place
no fmt --fix=unwrap -d src/std/str.no # print the diff only, do not write
no fmt --fix=unwrap src/std/str.no # print the fixed content
Fixed shape (see Safe Indexing): the enclosing function already has a ?T result param → propagate (x ?= <expr>); otherwise → rename + match unwrap:
; before
x = a + b
; after (the enclosing function has a ?T result param)
x ?= a + b
; after (otherwise: the 4-line canonical form; an empty catch-all arm = the zero value)
x-opt = a + b
x = x-opt: {
ok -> it
->
}
As with the other fix classes: surgical text replacement, no re-layout, and the #{overflow=…} / #{index-out=…} annotations above the statement are left alone (they still decide "what value to use on failure"). Re-running is idempotent. After fixing, run no vet once more to confirm optunuse reaches zero.
Annotation binding and single-line merging
#{...} annotations are bound to the node, so a single statement can carry several keys at once, and multiple
annotations are always merged onto one line:
#{fmt-skip}
i <- [0..16): {
#{index-out=0, overflow=wrap}
buf[base + i] = data[i]
}
This one statement needs two meanings at once: "out-of-range access yields the default" (index-out) and "on
overflow, wrap" (overflow). Writing them on two separate lines parses identically, but
no fmt --fix=overflow produces the merged single-line form.
Scope of a line annotation:
#{overflow = ...}applies only to the statement immediately following it. An unrelated annotation in between (such as#{index-out = 0}) does not stop it from reaching that statement. Safe indexed writes (arr[base + i] = v,.buf[.pos + i] = v) are desugared into a match form, and#{overflow = wrap}is carried over to the desugared node, so the index arithmetic is governed by the annotation too and is not misreported as unannotated.
Error Propagation (the ?= Operator)
?= is the concise shorthand for unwrapping an option and throwing the error upwards.
An option return type is not required: either of the two conditions below is enough, so a
function whose return type is not option can still write y ?= x + 1 by wrapping its body in a
block match.
Syntax:
v ?= expr
Semantics:
- If
exprreturnsok(value),vis assigned the unwrapped inner value (automatic unwrap) - If
exprreturnsnilorerr:- with a
?Tresult param → the option is written there and the function returns early; - inside a statement-level block match
{ ... }: { ... }→ the nearest block match captures it, the whole body stops and the arms run once (not areturn); no?Tresult param needed.
- with a
Available under either condition:
- The enclosing function has an option-typed result parameter; or
- The statement is inside the body of a statement-level block match
{ ... }: { ... }(see The Three Match Subject Forms).
Outside both, the compiler reports an error. The two-arm err -> _ / -> _ form discards the
propagated failure and is the standard fallback for functions without a ?T result param:
#{fmt-skip}
handle = (x i64) {
{
y ?= x + 1
print(1)
}: {
err -> _
-> _
}
}
No pre-initialization needed: the function prologue does not zero-initialize out parameters, but at return
time any out parameter that was never explicitly assigned is auto-filled with its type's zero value (integer →
0, str / struct → zeroinitializer, option → nil). That is why the examples below carry no result = nil
boilerplate — write only the success-path assignments.
Capture vs. propagate:
?=means "throw the error upward" — it requires an option result param and performs an earlyreturnon failure. If you want to handle the failure in place and keep going, use plain=instead (see Error Capture Assignment below).
Example:
#{fmt-skip}
; Read a file line by line and process it — using ?= to simplify error propagation
process-file = (path str) (result ?str) {
f ?= open(path) ; on failure, result = f automatically; return
line ?= f.read-line() ; EOF or error propagates automatically
result = line
}
The equivalent expanded form (the match chain the compiler generates automatically):
#{fmt-skip}
process-file = (path str) (result ?str) {
result = nil
__tmp = open(path)
__tmp: {
nil || err -> {
result = __tmp
return
}
-> f = it
}
__tmp2 = f.read-line()
__tmp2: {
nil || err -> {
result = __tmp2
return
}
-> line = it
}
result = line
}
Chained use: several ?= can be chained to achieve pipeline-style error propagation:
#{fmt-skip}
pipeline = (input str) (result ?str) {
a ?= step1(input) ; propagate on failure
b ?= step2(a) ; propagate on failure
result = b
}
Bare arithmetic: the right-hand side of ?= is not limited to function calls — any integer operation returning an
option (an unannotated a - b, a + b, a * b) can be propagated directly with ?=, without binding it to an
intermediate variable first:
#{fmt-skip}
; on overflow, result = err automatically; return; otherwise v unwraps to the inner value
sub-safe = (a i64, b i64) (result ?i64) {
v ?= a - b ; underflow/overflow → err propagates; otherwise v = the inner i64
result = v
}
add-safe = (a i64, b i64) (result ?i64) {
v ?= a + b ; same as above; applies to + * and signed /
result = v
}
Equivalent expansion (generated automatically by the compiler):
#{fmt-skip}
sub-safe = (a i64, b i64) (result ?i64) {
result = nil
__unwrap = a - b ; option<i64>
__unwrap: {
nil -> {
result = __unwrap
return
}
err -> {
result = __unwrap
return
}
-> v = it ; ok(value): unwrap the inner value
}
result = v
}
Compound RHS: the right-hand side of ?= may be a compound expression with several option operands. Every option
operand is unwrapped automatically and a propagation guard is injected — if any operand is nil / err, the current
function's option result parameter is set to that value and return runs (propagating upwards); only if all succeed is
the inner value assigned to the LHS:
#{fmt-skip}
sum3 = () (result ?i64) {
a ?i64 = ok(10)
b ?i64 = ok(20)
c ?i64 = ok(30)
total ?= a + b + c ; every option operand is unwrapped and propagated automatically
result = total
}
comp = () (result ?i64) {
a ?i64 = ok(4)
b ?i64 = ok(6)
v ?= dbl(a + b) ; a + b may be an option; it is likewise unwrapped and propagated automatically
result = v
}
Only options in an operand position are unwrapped automatically (e.g.
a + b + c,f(b + c)); an option variable passed directly as a function argument is not — it follows the existing bare-option-argument exemption for?=(a ?= f(v), wherevis?i64andftakes a plaini64).
Non-option RHS is an error: to avoid accidentally writing = as ?= on a large scale, when the right-hand side can
be determined at compile time to be "definitely not an option", the compiler reports an error and suggests using =:
- Literals:
v ?= 42 - Constant-foldable integer arithmetic:
v ?= 10 + 20 - A local variable of known, non-option type:
v ?= x(wherex i64 = 5)
The message looks like: `?=` RHS `42` is not an option — use `=` instead.
An arithmetic operation is treated as "possibly an option" — and therefore allowed — as soon as any operand is an option (or of unknown type, or a function call that may return an option), so there is no false positive: for example
v ?= a + b + c(a/b/c are?i64) orv ?= b + c(b/c of unknown type) both compile fine.
Applicable scenarios:
- Container operations that may be empty, such as
pop/peek→?t(nil= empty) - I/O operations such as
read-line/read-byte→?str/?i64(nil= EOF,err= error) - Lookup operations such as
lookup/get→?t(nil= not found) - Parsing operations such as
parse/from-str→?t(nil= empty,err= invalid input) - Network connections such as
accept/dial→?conn(nil= no connection,err= error)
nil vs err: use nil when the absence is a normal/expected result (empty stack, missing key, EOF); use err('msg') when the absence represents an actual error state (I/O failure, invalid input, connection refused).
Exception: when a function needs to return several independent values (such as (name str, value str, ok bool)), the multiple-return-value pattern may be kept.
Error Capture Assignment (capture with =)
?= means "throw the error upward" — it requires the enclosing function to have an option result param and returns early on failure. When you don't want an early return but would rather handle the error in place, use the ordinary =: as long as the right-hand side contains a fallible source, the target variable is inferred as ?T, and the nil / err is stored into that variable while flow continues.
#{fmt-skip}
handle = (b i64, c i64, d i64) (r i64) {
a = b + c / d ; a infers ?i64; divide-by-zero → a = err
a: {
err -> { r = -1 } ; handle the error in place
nil -> { r = -1 }
-> { r = it } ; success: r = b + c/d
}
}
Fallible sources (capture triggers):
| Source | Example | Capture result |
|---|---|---|
| Integer division / modulo | a = b + c / d, a = b % c | divide-by-zero, MIN / -1 → err |
| Safe index | a = v[i] (arr / vec / slice) | out of range → nil |
| Option-typed operand | a = x + 1 where x ?i64 | x is nil / err → stored as-is into a |
| Call returning an option (used as an arithmetic operand) | a = f(x) + 1 where f returns ?i64 | same as above |
An option passed directly as a function argument is not in this list — that is the callee's business (consistent with the bare-option-argument exemption for
?=).
Pure arithmetic is not a fallible source: + - * << and negation do not trigger capture on their own (otherwise every arithmetic expression would become an option). But once an expression lands on the option path (for example the RHS also contains /, or the target is explicitly declared ?T), those operations carry runtime overflow checks, and overflow → err:
#{fmt-skip}
ovf = (x i64) (r i64) {
a ?i64 = x + 1 ; explicit ?T → takes the option-wrap path
a: {
err -> { r = 0 } ; x = i64-max → overflow → err
nil -> { r = 0 }
-> { r = it }
}
}
Capture vs. propagate:
| Form | On failure | Needs an option result param | Reading the value later |
|---|---|---|---|
a ?= expr | sets the result param to nil/err and returns (early return) | yes | n/a (already returned) |
a = expr | stores nil/err into a in place; flow continues | no | a: { ok -> ... } |
a ?T = expr | same as = (explicit annotation; inner fallible subexpressions included) | no | same as above |
_ = expr | evaluates but discards both the value and the error (no error, no unused lint) | no | n/a |
Scope: capture from safe indexing — like ?= — fires only inside functions that return a ?T result (see Safe Indexing, form 3). Capture from / % and from option operands applies in all functions.
Existing variables keep their type: capture only applies to a target first declared by that statement. If the target already exists (v = arr[i] after v i64 = 0, or d = x - 1 after d = 0), the compiler does not change its type in place — it reports an error asking you to pick a semantics explicitly (add an #{overflow=...} / #{index-out=...} annotation, use ?=, or declare ?T).
Generics
arr_to_vec = (arr [n]t) (out []t) {
i <- [0..n): {
out[i] = arr[i]
}
}
Type Casting
; Return the type name string
a = typeof(x)
; `as` is only allowed for FFI pointer type casts (e.g. *byte, **byte, *i64)
; Integers are internally i64, no explicit cast needed
y = x as *byte
Integer Assignment Type Checking
The compiler type-checks integer assignments to prevent unsafe narrowing that could cause data loss.
Implicit Widening (safe, auto-allowed)
A narrower integer type's value can be auto-assigned to a wider type, since the target range fully contains the source range:
#{fmt-skip}
b byte = 200
i i64 = b ; ✓ byte range [0,255] ⊆ i64 range
u u32 = b ; ✓ byte range ⊆ u32 range
Integer Literal Assignment
Integer literals (default inferred as i64) can be assigned to any integer type whose range includes the literal value:
#{fmt-skip}
n u8 = 200 ; ✓ 200 ∈ [0,255]
m u8 = 300 ; ✗ 300 > 255, compile error
big u64 = 18446744073709551615 ; ✓ 2^64-1, u64 max
Unsafe Narrowing (compile error)
Assigning a wider-typed variable directly to a narrower type causes a compile error, as it may cause data loss. The error message includes an actionable fix hint suggesting how to narrow safely with bitwise operations:
#{fmt-skip}
d u64 = 42
h u32 = d ; ✗ cannot assign u64 value to u32 variable 'h'; hint: narrow safely with a bitwise mask (e.g. `& 4294967295`) or right shift (e.g. `>> 32`)
h u16 = d ; ✗ cannot assign u64 value to u16 variable 'h'; hint: narrow safely with a bitwise mask (e.g. `& 65535`) or right shift (e.g. `>> 48`)
h u8 = d ; ✗ cannot assign u64 value to u8 variable 'h'; hint: narrow safely with a bitwise mask (e.g. `& 255`) or right shift (e.g. `>> 56`)
x u32 = d + 1 ; ✗ addition result is still u64, unsafe
y u32 = foo() ; ✗ function call result type mismatch
Fix hint: The compiler auto-computes the exact mask value and shift amount for the target type. Apply the suggested mask or shift to narrow safely (see next section).
Signed target types: For
i8/i16/i32/i64, the hint explains that bitwise narrowing is not safe (sign-bit truncation is ambiguous) and suggests an explicit range check instead.
Safe Bitwise Narrowing (auto-allowed)
When the right-hand side of an assignment is a bitwise expression (&, |, ^, <<, >>) and the target type is an unsigned integer (u8/u16/u32/u64/byte), the compiler allows implicit narrowing — because high-bit truncation is the standard semantics of bitwise operations and does not cause unexpected data loss:
#{fmt-skip}
d u64 = 42
; ✓ mask operation: result ≤ mask value, safely fits u32
h u32 = d & 67108863 ; mask = 2^26-1 < 2^32
h u32 = d & 4294967295 ; mask = 2^32-1, exactly u32 range
; ✓ shift operation: high bits are 0 after right shift
hi u32 = d >> 32 ; u64 >> 32 leaves 32 bits
; ✓ XOR / OR combinations
c u32 = a ^ b ; bitwise operation result
b byte = v & 255 ; mask to byte range
; ✓ composite bitwise (common in crypto/codec)
s u32 = (key[0] & 255) | ((key[1] & 255) << 8) | ((key[2] & 255) << 16) | ((key[3] & 255) << 24)
Why allowed? Bitwise operations (mask, shift, XOR, OR) semantically construct a bit pattern. Assigning to a narrower unsigned type truncates the high bits intentionally — the developer has already ensured the result's range via mask or shift, or deliberately discards high bits. This is a standard pattern in cryptography (e.g. ChaCha20, Poly1305, Blake2) and codec code.
Unsigned target types only. For signed integer targets (
i8/i16/i32/i64), even with a bitwise RHS, an error is still reported because sign-bit truncation semantics are ambiguous:d u64 = 42h i32 = d & 4294967295 ; ✗ still errors: signed target not eligible
Top-level must be a bitwise op. Only when the expression's top-level operator is
&/|/^/<</>>is it allowed. Addition, subtraction, function calls, direct variable references, etc. are not covered:d u64 = 42h u32 = d ; ✗ top-level is Identifier, not bitwiseh u32 = d + 1 ; ✗ top-level is +, not bitwise
Module System
- Each file is a module
- File names and folder names use hyphens
utils/
└── helper.no ; module name is utils/helper
Importing Modules
The new syntax uses
#for imports. The oldusekeyword is still available but deprecated; switching to#is recommended.
; Standard library (new syntax, recommended)
# std/math.add
; Remote module (does not start with std/)
# github.com/utils/math.add
; Local module; must start with /
# /utils/math.add
; Alias
# std/math.add a
; ── The following is the old syntax (deprecated, still usable but not recommended) ──
; use std/math.add
; use github.com/utils/math.add
; use /utils/math.add
; use std/math.add a
Path forms:
# /mod(with a space after#, immediately followed by/) is a module of the current package, resolved from the containingpackage.noml. It is distinct from# mod/mod(package-name path) and the no-space#/mod(rejected by the CLI).@ /modfollows the same rule for exports;no fmtpreserves the spaced form.
Importing .noml Configuration Files Directly
A .noml file can be imported with #: resolution looks for <name>.no first and falls
back to <name>.noml. After the import, the struct name defaults to the file name
(without the extension, overridable by a #{name = '...'} annotation at the top of the
file) and each member becomes a field of that struct. A same-named global value holds
the file contents, so you read configuration with server.port.
; cfg/server.noml
{
host: 'localhost'
port: 8080
tags: [] #{type = '[]str'}
compiler: {
version: 'v0.1.0'
}
}
#{fmt-skip}
# /cfg/server
print(server.port) ; 8080
print(server.compiler.version) ; v0.1.0
- A field's type comes from the member's
#{type = '...'}annotation (the value is a full nolang type expression such as'[]str','[]i64','?i64'). Without an annotation it is inferred from the value: string →str, integer →i64, decimal →f64, bool →bool, object → a generated nested struct (server-compiler), array →[]element-type. - Empty containers (
[],{}) must carry#{type = '...'}: noml values are untyped, so an empty container cannot be inferred. When an object member is annotated with#{type = 'x'}the annotation is used as-is (xmust be defined by you) and no nested struct is generated. - Nested struct names are prefixed with the file name (
server-compiler); nested types are defined before their host type. - A top-level (file-level) annotation can rename the struct:
#{name = '...'}(the equivalent spelling#{type = '...'},namewins) sits on its own line above the top level{, or trails on the top value's line. The value binding and the nested struct prefix follow the new name, while the import path is still resolved from the file name:
; cfg/db.noml
#{name = 'db-options'}
{
host: 'localhost'
}
#{fmt-skip}
# /cfg/db
# /cfg/db as db2
print(db-options.host) ; localhost
print(db2.host) ; localhost
- Two different files that would produce the same type name (e.g. same-named files in
different directories) are reported as an error — tell them apart with a top-level
#{name = '...'}. Importing the same file more than once (e.g. again with anasalias) merges the type definition only once. - Keys are converted to lower-case hyphenated nolang names (
anonymous-fn-type): a leading digit getsf-, and a name equal to a keyword getsx-. - An
asalias renames the value binding only; the struct name stays the file name:# /cfg/server as cfg. .nomltypes never take a module prefix: even when the file name equals a standard library module (e.g.server), the type stays bare.package.noml/workspace.nomlalso accept#{...}annotations (e.g.#{type = '[]str'}): they are ignored at parse time and only serve external tools that rebuild a strongly typed structure; the syntax is identical to nolang's#{...}.
Exporting Modules
Only applies to lib.no
@ std/math.add a
FFI (#{c} Annotation)
Declare external C functions via the #{c} annotation to implement FFI (Foreign Function Interface).
Syntax: #{c} stands on its own line and marks the next line as an FFI declaration. #{c} is the FFI language key of the annotation system; #{cpp}, #{rust}, and other languages are also supported. The old syntax #c remains backward compatible.
Private declarations: A name starting with _ is private (not exported); the C ABI symbol automatically drops the _ prefix and converts hyphens to underscores.
No longer requires separate files: FFI declarations and ordinary code can be written in the same .no file.
Pointer type syntax: FFI uses C-style *T, **T, ***T to denote pointers, and a concrete type T is required. Ordinary code cannot use this syntax.
| Syntax | Meaning | LLVM IR | Purpose |
|---|---|---|---|
*byte | pointer to byte | i8* | opaque pointer (e.g., db handle) |
**byte | double pointer | i8** | output parameter (e.g., sqlite3**) |
***byte | triple pointer | i8*** | rare triple indirection |
; sqlite.no — FFI bindings and safe wrappers in the same file
; The compiler automatically converts hyphens (-) to underscores (_) to match C ABI symbols
; Names starting with _ are private; the C ABI symbol automatically drops the _ prefix
; Basic type parameters
#{c}
c-strlen = (s str) (n i64)
; Pointer parameter (*byte = opaque pointer), private declaration
#{c}
_sqlite3-close = (db *byte) (rc i32)
; Double pointer (**byte = output parameter; after the call, the value is automatically stored back into the variable), private declaration
#{c}
_sqlite3-open = (filename str, db **byte) (rc i32)
; Multiple pointer parameters, private declaration
#{c}
_sqlite3-exec = (db *byte, sql str, callback *byte, arg *byte, errmsg *byte) (rc i32)
; Safe wrapper in the same file
open = (dsn str) (d db-sqlite) {
handle i64 = 0
rc i32 = _sqlite3-open(dsn, handle)
rc != SQLITE-OK -> {
return
}
d.handle = handle
}
Rules:
#{c}stands on its own line and marks the next line as an FFI declaration (the old syntax#cremains backward compatible)- An FFI is only a declaration, with no function body
- Pointers must have a concrete type (e.g.,
*byte); bareptris not allowed **byteis used for output parameters: after the call, the pointer value written by the C function is automatically converted toi64and stored back into the caller's variable- All pointers are stored as
i64on the Nolang side (ptrtoint) strtype parameters are automatically converted to null-terminatedi8*- A name starting with
_is private (not exported); the C ABI symbol drops the_prefix - FFI declarations and ordinary code can be written in the same
.nofile
The reverse direction (having C / C++ / Rust / Go / Java / Python / C# call into Nolang) uses
#{cabi='symbol'}withno build --shared; see Calling Nolang From Host Languages.
Annotation System (#{...})
#{...} is a general annotation system, a comma-separated list of key-value pairs. It supports the following value types:
| Syntax | Type | Example |
|---|---|---|
| Standalone key | Boolean | #{debug} |
| Boolean | Boolean | #{inline=true} / #{inline=false} |
| Number | Integer | #{max=100} |
| Text | String | #{name='hello'} |
| Identifier | Identifier | #{mode=fast} |
| Array | Array | #{derive=[Serialize, Deserialize]} |
| Range | Range | #{range=[0..256)} |
Multiple key-value pairs are separated by commas:
#{derive=[Serialize, Deserialize], range=[0..256), max=100, debug}
Range syntax supports 16 bracket combinations (the rule: [ includes the left endpoint, ( excludes it; ] includes the right endpoint, ) excludes it):
Bounded (4) — both ends have a value:
[a..b]— closed at both ends (x >= a && x <= b)[a..b)— left-closed, right-open (x >= a && x < b)(a..b]— left-open, right-closed (x > a && x <= b)(a..b)— open at both ends (x > a && x < b)
No upper bound (4) — the right endpoint is omitted:
[a..],[a..)— left-inclusive, no upper bound (x >= a)(a..],(a..)— left-exclusive, no upper bound (x > a)
No lower bound (4) — the left endpoint is omitted:
[..b],(..b]— no lower bound, right-inclusive (x <= b)[..b),(..b)— no lower bound, right-exclusive (x < b)
Fully unbounded (4) — both ends omitted, based on the type's minimum/maximum:
[..]— type min ≤ x ≤ type max (both ends inclusive)[..)— type min ≤ x < type max(..]— type min < x ≤ type max(..)— type min < x < type max
Annotation Placement
A #{...} group may be written in exactly two places:
- Above — on its own line, directly above the target (statement, struct field, enum member, declaration, match arm);
- Trailing — on the target's same line, after it.
A prefix annotation (#{index-out=zero} res = arr[i]) — one written on the same line in front of
its target — is a compile error, reported identically by the compiler and by nolang-lsp. The rule
is decided the same way everywhere: if code still follows the group's closing } on the same line,
it is a prefix. A newline, a ; / // line comment, a closing }, or another #{ group does
not count as code.
; ✓ above: on its own line
#{overflow=wrap}
x i8 = a + 100
; ✓ trailing: at the end of the target's line
x i8 = a + 100 #{overflow=wrap}
#{fmt-skip}
; ✗ prefix: same line, in front -> compile error
#{overflow = wrap} x i8 = a + 100
Two consequences that are easy to miss:
- A trailing annotation belongs to the statement it trails, not to the one that follows it.
if <cond> #{...} {is a prefix position too, and is an error; that spot used to be silently swallowed (no effect and no error), which made it the hardest form of failure to notice.
Struct fields and enum members (the values of a C-style enum, the variants of a tagged enum) follow
the same rule; the trailing spelling is the conventional one: p pt #{inline}, p pt #{inline=true},
green #{deprecated}, ok(v i64) #{inline}. The value of #{inline} is supported too —
#{inline=false} is legal (see #{inline} on Struct Fields).
The FFI annotation #{c} is a special form of the annotation system. When an annotation contains an FFI language key (c, cpp, rust, etc.) and is followed by a function declaration, the compiler identifies it as an FFI binding:
; #{c} with additional annotations
#{c, debug}
_sqlite3-open = (filename str, db **byte) (rc i32)
Annotations Attached to Declarations
Non-FFI annotations are automatically attached to the declaration that immediately follows (variable declarations, struct definitions) and can be used to tag metadata such as range limits for numeric types (e.g., num, i64, etc.):
; Variable declaration with a range annotation
#{range=[0..256)}
x num = 42
; Struct definition with annotations
#{derive=[Serialize, Deserialize]}
point {
x i64
y i64
}
; Struct field with a range annotation (can be used for numeric types such as num)
person {
age num #{range=[0..150]}
score i64 #{range=[0..256)}
name str
}
The range annotation is especially suited to the num type (num = int | float) for marking the valid range of a numeric value. Range values can be integers or identifiers:
; Use constant identifiers as range bounds
#{range=['i8.MIN'..'i8.MAX']}
val i8 = 100
File Embedding (#{embed=...})
The #{embed=...} annotation embeds external file contents as a []byte read-only constant at compile time, similar to Go's //go:embed. The embedded data is compiled directly into the executable, enabling single-binary distribution without runtime file I/O.
Syntax:
#{embed='path/to/file'}
ICON []byte
; or using a bare path (without quotes)
#{embed='assets/icon.ico'}
ICON []byte
The embed annotation attaches to the []byte variable declaration that immediately follows. The declaration must not have an explicit initial value — the value is provided by the embedded file contents.
Path Resolution:
- Relative paths are resolved relative to the package root (the directory containing
package.noml) - Absolute paths are supported
- Paths may contain
/,., and-characters
Example:
; Embed a Windows icon
#{embed='assets/win-icon.ico'}
WIN-ICON []byte
; Embed an SSL certificate
#{embed='certs/server.pem'}
SERVER-CERT []byte
; Embed a configuration file
#{embed='config/default.json'}
CONFIG []byte
; Use embedded data
print('icon size: ', len(WIN-ICON))
print('first byte: ', WIN-ICON[0])
Notes:
- Embedded data is read-only constant, pointing to read-only memory. Writing to embedded data is undefined behavior (same semantics as Go's
//go:embed) - Embedded variables are excluded from heap cleanup — the embedded data pointer is not freed on program exit
- Only
[]bytetype declarations are supported ([N]bytefixed arrays also work) - The file must exist at compile time, otherwise an error is reported
- Cannot be combined with an explicit initial value (e.g.,
ICON []byte = [1, 2, 3]will error)
Directory Embedding (#{embed='dir'})
The #{embed=...} annotation also supports embedding an entire directory. When the path points to a directory, the compiler recursively reads all files and embeds them as an fs.embed read-only filesystem. At runtime, files are accessed via read() and exists() methods — the lookup logic is implemented entirely in Nolang, with no C functions.
Syntax:
; Embed an entire frontend directory
#{embed='../frontend/dist'}
DIST fs.embed
; Read a file from the embedded filesystem
data = DIST.read('index.html')
; Check if a file exists
ok = DIST.exists('css/style.css')
fs.embed type:
fs.embed is a struct defined in the Nolang standard library fs module, containing metadata for embedded files (paths, offsets, lengths). The compiler reads the directory at compile time and stores all file contents as constant global variables. The read method is implemented in pure Nolang.
| Method | Signature | Description |
|---|---|---|
read | (path str) (data ?[]byte) | Read an embedded file, returns its content (nil when absent) |
exists | (path str) (yes bool) | Check if an embedded file exists |
Example:
; HTTP static file server: embed frontend files into single binary
#{embed='../frontend/dist'}
DIST fs.embed
serve = (conn server-conn) {
conn.parse()
path = conn.path
path == '/' -> path = '/index.html'
; Strip leading / to get relative path
rel = path.slice(1, path.len-bytes())
; Read from embedded filesystem
data []byte = DIST.read(rel)
{
ok -> it
-> {
conn.write-html(404, 'Not Found')
conn.close()
return
}
}
conn.write-status(200)
conn.write-header('Content-Type', get-content-type(path))
conn.write-body(data)
conn.close()
}
Notes:
- All files in the directory (including subdirectories) are recursively embedded, with paths stored relative to the embed directory (e.g.
css/style.css) - Path separators are normalized to forward slashes
/(cross-platform consistent) fs.embedtype variables are read-only and excluded from heap cleanup- Suitable for single-binary distribution scenarios (e.g. HTTP static file servers, CLI tools with embedded resources)
Platform Annotations
Platform annotations are compile-time filters that include or exclude code based on the target platform. They use flattened keys that unambiguously specify both OS and architecture (e.g. #{mac-arm64}), and are attached to the declaration that follows. Non-matching code is excluded from the build entirely — no LLVM IR is generated, no type checking is performed.
Supported platform keys (6 flattened combinations):
| Key | Matches |
|---|---|
#{linux-amd64} | Linux on x86_64 |
#{linux-arm64} | Linux on ARM64 |
#{win-amd64} | Windows on x86_64 |
#{win-arm64} | Windows on ARM64 |
#{mac-amd64} | macOS on x86_64 (Intel) |
#{mac-arm64} | macOS on ARM64 (Apple Silicon) |
; Platform-specific print
#{mac-arm64}
print('running on macOS ARM64')
#{linux-amd64}
print('running on Linux x86_64')
#{win-amd64}
print('running on Windows x86_64')
; Platform-specific variable
#{mac-amd64, mac-arm64}
sep = '/'
#{win-amd64, win-arm64}
sep = '\\'
; Platform-specific function
#{mac-arm64, mac-amd64}
greet = () {
print('hello from mac')
}
#{linux-amd64, linux-arm64}
greet = () {
print('hello from linux')
}
greet()
Multiple keys on the same declaration are OR'd together — any match includes the code. No AND logic is needed because each key already specifies both OS and arch.
| Annotation | Meaning |
|---|---|
#{mac-arm64} | macOS ARM64 only |
#{mac-amd64, mac-arm64} | macOS on any arch |
#{linux-amd64, win-amd64} | Linux x86_64 or Windows x86_64 |
#{mac-arm64, linux-arm64} | macOS ARM64 or Linux ARM64 |
; Included on both macOS and Linux (all archs)
#{mac-amd64, mac-arm64, linux-amd64, linux-arm64}
shared = () {
print('unix-like')
}
; Only on Windows x86_64
#{win-amd64}
reg-key = () {
print('reading registry on win/x64')
}
; Only on macOS ARM64 (Apple Silicon)
#{mac-arm64}
neural = () {
print('Apple Neural Engine available')
}
Use os.get-arch() to get the current architecture at runtime, and platform annotations to include/exclude code at compile time.
package.noml Compiler Configuration
The compiler block in package.noml controls compiler behavior. The version value is conventionally written with a leading v (e.g. "v0.1.0"); the compiler's internal parsing is prefix-insensitive, treating "v0.1.0" and "0.1.0" as the same version.
{
compiler: {
version: 'v0.1.0'
emit: 'js'
}
}
emit
Controls the output target backend:
| Value | Description |
|---|---|
"" (empty, default) | Use LLVM backend to generate native executable |
"js" | Use JS backend to emit JavaScript (type erasure), no LLVM toolchain required |
When set to "js", no build automatically uses the JS backend, equivalent to the --js command-line flag. Command-line flags take precedence over config file settings.
; Automatically generate JavaScript
compiler: {
emit: 'js'
}
anonymous-fn-type
Controls whether anonymous function type syntax (e.g. cb ()()) is permitted. Defaults to false (disabled), requiring named function type aliases.
link-libs
Specifies a list of C libraries to link:
compiler: {
link-libs: ['crypto', 'ssl']
}
Compile-time Variable Injection (-ld)
no build, no run, and no test support injecting compile-time global constants via -ld-KEY=VALUE flags. Injected variables are equivalent to declaring KEY = VALUE at the top of the source file and can be used directly in the program.
Syntax
no build -ld-KEY=VALUE [more -ld...] <file>
- The
-ldprefix is immediately followed by the variable name and value, separated by= - Multiple
-ldflags can be used simultaneously - Boolean shorthand:
-ld-DEBUG(without=VALUE) is equivalent to-ld-DEBUG=true
Value Type Inference
| Value form | Inferred type | Example |
|---|---|---|
| Integer | i64 | -ld-COUNT=42 |
| Float | f64 | -ld-PI=3.14 |
true / false | bool | -ld-DEBUG=true |
| Other | str (single-quoted string literal) | -ld-VERSION=0.1.2 |
Examples
#{fmt-skip}
; Assuming compilation with -ld-VERSION=0.1.2 -ld-COUNT=42 -ld-DEBUG=true
print('VERSION:', VERSION) ; Output: VERSION: 0.1.2
print('COUNT:', COUNT) ; Output: COUNT: 42
print('DEBUG:', DEBUG) ; Output: DEBUG: 1 (bool prints as 1 on native backend)
; Inject multiple variables
no build -ld-VERSION=0.1.2 -ld-COUNT=42 -ld-DEBUG=true main.no
; Boolean shorthand
no run -ld-RELEASE main.no
; JS backend also supported
no build --js -ld-VERSION=0.1.2 main.no
Relationship with Source Declarations
-ld injected variables interact with source declarations as follows:
- Source already declares the same variable: the injected value replaces the source value in-place. A common pattern is to declare a placeholder in source (e.g.
VERSION = '') and assign it at build time with-ld-VERSION=0.1.2. - Variable does not exist in source: the injected variable is prepended to the AST as a new global constant declaration, participating in all subsequent compilation passes (type inference, validation, module merge, codegen).
- Not injected: the source declaration remains unchanged.
; Source declares placeholder variables
VERSION = ''
COUNT = 0
print('VERSION:', VERSION)
print('COUNT:', COUNT)
; no build -ld-VERSION=0.1.2 main.no
; Output: VERSION: 0.1.2 (injected value replaces source '')
; COUNT: 0 (not injected, source value preserved)