Library
The standard library
The prelude that ships with the compiler, written in the language itself and in scope in every program.
since 0.1.0-alpha.1linuxwindowswasm
Description
The standard library is called the prelude. It is a set of ordinary source files written in Leviathan, on top of a small set of native operations, and it ships inside the compiler. You do not install it: every program is compiled together with it.
Its top-level types are the core language and are in scope from the first line: the primitives,
string, Array, Map, Set, Pair, the iterators, Seq, Range, the stream types,
StringBuilder, the exception hierarchy, IDisposable and Promise. Everything else lives in a
namespace, and each namespace is a library a file imports before it uses it (see
imports): io (the console: use io::console;), std (files, sockets, HTTP,
processes, timers, the system calls: use std::File; or uses std;), math (use math; for
math::pi), regex, json, datetime, env and the rest. Because the prelude is written in the
language, you can read its sources as examples of ordinary Leviathan code.
One part of the prelude is only present for one target. The browser interface of the WebAssembly target is loaded when you build for a WebAssembly target and is invisible to native builds.
A little of everything, with no imports
use io::console;
use math;
use regex::Regex;
Array<string> words = ["pear", "fig", "apple"];
Map<string, int> lengths = Map();
for (string w in words) { lengths = lengths.with(w, w.length()); }
console.writeln(lengths);
Set<int> sizes;
sizes = Set(lengths.values());
console.writeln(sizes.length());
console.writeln(math::max(lengths.at("fig"), lengths.at("pear")));
Regex vowels = Regex("[aeiou]");
console.writeln(vowels.count("apple"));
StringBuilder sb = StringBuilder();
for (int i in 1..3) { sb.add(i.toString()); }
console.writeln(sb.toString());
{pear: 4, fig: 3, apple: 5}
3
4
2
123
Where things are
This chapter's companion pages cover the core types.
- Collections.
Array<T>,Map<K, V>andSet<T>are pure values; seelang.collection-value-semanticsfor the shared rule.Rangeis the value ofa..b.PairandTripleare small value types, andOrderedArrayis the result of a multi-key sort. - Iteration. The
IIterableandIIteratorinterfaces and thefor-inrules are inlang.iterator-protocol, andSeqis the lazy pipeline built on them. - Text tools.
StringBuilderaccumulates a long string, andRegexwith theregexnamespace does pattern matching in time linear in the input. - Numbers. The
mathnamespace holds the constants and the transcendental functions.
The rest of the library has its own pages: the primitive types with their methods, Promise and
tasks, streams and files, networking, Console, byte buffers (Block), processes and terminals.
The generated library reference lists every type and method with its signature and examples.
Rules
- No import is needed to use the prelude;
stdis implicitly in scope. - A prelude name can be written bare (
sum) or qualified (std::sum). math,regexandenvare namespaces of their own and are written with their own prefix:math::pi,regex::isMatch(...).- A top-level class may not take a built-in type's name (
Array,Map,Range: the core language,lang.imports). A namespace's class of that name is an ordinary class, andfor-inover it uses its owniterator(): the loop recognises the built-in types by their declarations. - The standard library is resolved and checked before your program and apart from it. A name your
program declares at top level, a member you add to a standard-library namespace, or a top-level
bindis never visible to standard-library code. Your program sees the standard library exactly as before, except where it declares a name of its own: the standard library is an outer scope of every program, at every path, so the program's declaration hides the library's for the program's code and never clashes with it (lang.imports). Dynamic dispatch is unaffected: when your class overrides a method, standard-library code that calls that method through the base class reaches your override.
Examples
Bare and qualified names
use io::console;
use math;
use std;
use std::sum;
console.writeln(sum([1, 2, 3]));
console.writeln(std::sum([1, 2, 3]));
console.writeln(std::min([5, 3]));
Pair<int, int> p = Pair::Of(1, 2);
console.writeln(p.toString());
console.writeln(math::max(1, 2));
6
6
3
(1, 2)
2
A program may name its own class File. The standard library's csv code still reads through its own
std::File:
Your names do not reach the standard library
use csv;
use io::console;
use std;
class File {
string label = "the program's own File";
}
void readRows() {
std::File input = std::File("/dev/stdin", std::read);
for (Array<string> row in csv::rows(input)) {
console.writeln(row.joinToString("|"));
}
input.close();
}
console.writeln(File().label);
readRows();
id,name
7,Ada
the program's own File
id|name
7|Ada
See also
- Array — An ordered sequence of values of one type,
Array<T>, with value semantics. - Map — An associative collection from keys of type
Kto values of typeV, with value semantics. - Set — A collection of distinct values of type
T, with value semantics. - Range — A run of consecutive integers written
a..b, including both ends. - Pair — A pair of two values, possibly of different types.
- Triple — A group of three values, possibly of different types.
- StringBuilder — A mutable accumulator for building a string from many pieces.
- OrderedArray — An array that has been sorted by one or more keys, produced by
Array.orderByandthenBy. - Seq — A lazy sequence: a pipeline of steps that does no work until its result is requested.
- Regex — A compiled regular expression.
- math — Mathematical constants and functions on
float. - Promise — A value that arrives later.
- File — An open file.
- Console — The program's standard output as an object.
- Block — A fixed-length, mutable buffer of bytes.
- Collections are values — Array, Map and Set are values; the in-place verbs (add, set, remove, ...) change the variable you call them on, the prepositional forms (with, without, concat) return a new value.
- The iterator protocol — IIterable and IIterator — Two interfaces that make any type usable in a
for-inloop, and the rules for which loop path the compiler picks.