Prelude
The prelude declares the names every module sees without an import: the output functions, panic and assert, the conversions, len, a few types, and the namespaces of the standard library, such as Array, String, File, and Time. This page covers the names that stand on their own; each namespace has its own page.
Details
- No declaration, in any scope, may take a prelude name, so a local cannot be named
len,string, orprint. - A namespace such as
Os,Time, orReflectis reached only through.. It is not a value. assertis not a value either: it can only be called by name.- A conversion to an integer type panics rather than lose a value: out of range, a fraction,
NaN, or an infinity.float32andfloat64round and never panic. ant docshows the declaration and doc comment of any prelude name, such asant doc println.
Basic examples
println writes a value and a newline:
let name = "Ant"
println("Hello, ${name}")
println([1, 2, 3])Hello, Ant
[1, 2, 3]len counts, and a conversion changes a number's type:
len([10, 20, 30]) // 3
len("héllo") // 5
int8(100) // 100
float64(7) / 2.0 // 3.5assert stops the program when a condition is false:
let total = 2 + 2
assert(total == 4, "arithmetic")Scope
The functions of a namespace are called through it, through . on the value, or down a pipe:
let words = ["ant", "bee"]
Array.map(words, { String.toUpper(x) }) // ["ANT", "BEE"]
words.map({ x.toUpper() }) // ["ANT", "BEE"]
words | Array.map({ len(x) }) // [3, 3]Result, Ok, Err, and Error are the types the library reports failure with:
let r: Result[int, Error] = Ok{ value: 2 }
r is Ok // true
let e: Result[int, Error] = Err{ error: Error{ message: "no" } }
e.unwrapOr(0) // 0Integer, Number, and Ordered bound a type parameter, so one function takes every number type:
func half[T: Number](x: T): float64 {
float64(x) / 2.0
}
half(3) // 1.5
half(int8(5)) // 2.5
half(0.5) // 0.25A map yields Entry records, and Array.entries yields Indexed ones:
let ages = Map{ "Ann": 31, "Bob": 27 }
ages.entries() // [Entry{ key: "Ann", value: 31 }, Entry{ key: "Bob", value: 27 }]
["a"].entries() // [Indexed{ index: 0, value: "a" }]A conversion also unwraps a named primitive type:
type Count = int
let c = Count(3)
int(c) + 1 // 4Names by job
Output
- print: write a value to standard output, with no newline
- println: write a value and a newline
- eprint, eprintln: the same, to standard error
Stopping
Size
- len: the size of an array, a map, a set, a string,
Bytes, or a channel
Conversions
- int, int8, int16, int32, int64: to a signed integer, exactly
- byte, uint8, uint16, uint32, uint64: to an unsigned integer, exactly
- float32, float64: to a float, rounding to the nearest value
- string, bool: the payload of a named primitive type
Types
- Result, with
OkandErr: a value or an error - Error: the error the library returns, a message
- Panic and Panic.Frame: a panic that Panic.catch stopped
- Entry: a key and its value
- Indexed: an element and its index
- Seq: a sequence, which a
forloop iterates - Integer, Number, Ordered: bounds for a type parameter
Namespaces
The standard library lists every namespace, from Array and String to File and Http.
Applications
Report a problem on standard error and the result on standard output:
let scores = [72, 95, 88]
if len(scores) < 5 {
eprintln("warning: only ${len(scores)} scores")
}
println(scores.sum() ?? 0)255Turn a fraction into a percentage, rounding before the exact conversion:
let done = 7
let total = 9
int(Math.round(float64(done) / float64(total) * 100.0)) // 78Properties and relations
len agrees with the size function of each namespace:
assert(len([1, 2]) == Array.length([1, 2]))
assert(len("héllo") == String.length("héllo"))
assert(len(Map{ "a": 1 }) == Map.size(Map{ "a": 1 }))A call through . is the call through the namespace:
let s = " hi "
assert(s.trim() == String.trim(s))Possible issues
A namespace is not a value, so it cannot be stored:
let t = Time // error: Time is a namespace, not a valueNeither can assert:
let check = assert
// error: assert is not a value; it can only be called, as assert(cond) or assert(cond, message)A prelude name cannot be redeclared, even as a local:
let len = 3 // error: len is a prelude name and cannot be redeclaredA conversion to an integer panics on a fraction. Round first:
int(3.5) // panic: 3.5 has a fraction, so it does not convert to int
int(Math.round(3.5)) // 4len takes only collections, strings, Bytes, and channels:
type Point = { x: int, y: int }
let n = len(Point{ x: 1, y: 2 })
// error: len needs an array, a map, a set, a string, Bytes, or a channel, got PointSee also
Standard library, Modules, Errors, ant doc, Calls through ., Pipe
