Language reference
The language reference has one page for each keyword, operator, literal, and built-in type, and for each rule of how source text is read. This page lists them all, one line each. The functions of the standard library are in the library reference.
A few principles run through every page:
- Every construct is an expression with a type.
ifand blocks give values; a statement is an expression whose value isvoid. - No value changes type without a visible conversion. A condition is a
bool, and numbers of two types never mix. - Errors are values: a
Resultcarries an expected failure andnullcarries absence. A panic is for bugs. - When two constructs would do the same job, the language keeps one.
let n = 7
let parity = if n % 2 == 0 { "even" } else { "odd" }
parity // "odd"
float64(n) / 2.0 // 3.5
let name: string | null = null
name ?? "anonymous" // "anonymous"Lexical structure
Source files are UTF-8 text with the extension .ant.
- Comments and doc comments:
//and/* */comments, and the doc comments thatant docshows. - Identifiers: the names of variables, functions, types, parameters, and fields.
- Keywords and reserved words: the words that cannot name a variable, function, type, or parameter.
- Line endings: a newline ends a statement, and a few signs continue it onto the next line.
Literals
- Number literals:
42,0xFF, and1_000areint;3.14and1e9arefloat64. - Duration literals:
500ms,2h30m, and1.5sareTime.Duration. - String literals and interpolation:
"Hello, ${name}", with escapes. - Raw strings:
`C:\temp`, with no escapes and no interpolation. - true and false: the two values of
bool. - null: marks an absent value.
- Array literals:
[1, 2, 3], and...to spread another array. - Map literals:
Map{ key: value }. - Set literals: there are none;
Set.of([1, 2, 3])builds a set. - Record literals:
Point{ x: 1, y: 2 }, with field shorthand and.... - Lambdas:
{ x * 2 }as a function value, and anonymousfunc. - Regular expression literals:
/^\d+$/and/ant/i. - HTML expressions:
<p>Hello, {name}</p>builds anHtmlvalue.
Keywords
Declarations
- let: a binding that keeps its value, and record and array destructuring.
- var: a binding that can be assigned again, with the same destructuring as
let. - func: a named function, an anonymous function, or a function on a type.
- type: a record type, a named primitive type, or an alias, and
type(T), a type as a value. - interface: a named set of function signatures that a type satisfies.
- import: the exports of another module, as names or as a namespace.
- export: a top-level declaration of a library that other modules can import.
- test: a test or subtest in a test file, which
ant testruns.
Control flow
- if and else: one of two branches by a
bool, as an expression. - for and in: a block for each element of an array, map, set, string,
Bytes, or sequence. - while: a block as long as a condition is
true. - break: ends the innermost loop.
- continue: starts the next iteration of the innermost loop.
- return: leaves the innermost function, lambda, script body, or
goordeferblock early, with a value or none.
Errors and concurrency
- try: unwraps a
Result, or returns its error from the function. - defer: a block that runs when the enclosing block exits.
- go: runs a block on a new goroutine.
- select: waits for the first of several blocking calls that can proceed.
true, false, and null are under Literals, is under Operators, and void under Built-in types. panic, assert, len, println, and the conversions such as float64(n) are prelude functions, in the library reference.
Operators
- Arithmetic:
+,-,*,/,%, and unary-;+also joins strings. - Comparison:
<,<=,>, and>=on numbers and strings. - Equality:
==and!=, which compare structure. - Logical:
&&,||, and!onbool. - is: tests the member of a union and narrows to it.
- Null coalescing:
a ?? breplacesnull. - Optional chaining:
a?.b,a?.[i], andf?.(x)through a value that may benull. - Indexing:
a[i]on an array,Bytes, or a map. - Calls through .:
x.f(a)calls a field, orfof the namespace ofx's type. - The pipe:
a | f(b)callsf(a, b). - Assignment:
=,+=,-=,*=,/=, and%=. - Operator precedence: which operator takes an operand first.
Bitwise operations are functions of the Bits namespace, not operators.
Built-in types
- bool:
trueandfalse. - int and the sized integers:
int, andint8toint64anduint8touint64. - byte: another name for
uint8, the element ofBytes. - float32 and float64: IEEE 754 floating-point numbers.
- string: immutable Unicode text.
- void: the type and value of code that runs for its effect.
- never: the type of an expression that does not finish.
- Array:
Array[T], an ordered, growable list. - Map:
Map[K, V], a hash map in insertion order. - Set:
Set[T], distinct values in insertion order. - Function types:
func(A, B): R. - Self: the type that satisfies an interface, in its signatures.
null is the type of null. Bytes is built in for binary data. Records, unions, and generics have chapters of their own in the guide: Records, Unions, and Generics.
At run time
- Runtime panics: every way a checked program can panic.
