.nib std
nib 0.3.0

The nib standard library

Every type, function, interface and effector that ships with nibc, with the signature the compiler checks and an example it ran.

Every module is a namespace: its functions are called as Vec.size(v) or Http.parse(raw). Some are built into the compiler and always in scope — Vec, Map, String and the other core types — so they need no import. The rest are nib source files that ship beside the compiler, and a program opts into one with a use line before anything else in the file; each module's page shows which it is.

use "std/option.nib"

module Example where
  func main() =
    Option.unwrap_or(Option.None, 42)

wire W runs Example.main
42

Each item on these pages carries a label saying what kind of thing it is:

  • module — a namespace and everything in it. A builtin module like Vec is named for the type its functions work on.
  • type — a primitive type such as Int or Bool: literals and operators, and no module of functions.
  • data — a record or a sum declared with data; constructors are written Option.Some(1), matched as Some(x).
  • interface — a named capability. A function performs one of its operations with @Io.Print.println(…), and says so in its effect row (! Io.Print).
  • effector — the thing that answers an interface. A builtin instance like Io.Stdout is installed by nibc; you bind it to the interface in a wire: Io.Print = Io.Stdout.
  • func — a function. Its signature lists the parameter types, the answer after ->, the interfaces it may perform after !, and the errors it may raise after raises.

How to read an example

Unless it is a whole file, like the one above, an example is the body of a function, placed in this file and run with nibc --run:

module Example where
  func main() =
    <the example>

wire W runs Example.main

The dark strip under it is what nibc printed: the function's answer, plus anything it printed along the way. An example that can raise also gets a raises row on main, and one that performs an effect gets the wire binding it needs. All of them were compiled and run against the compiler version shown at the top of the page when this page was built.

A type variable such as 'a or 't stands for any type, the same one everywhere it appears in a signature: Vec.get :: Vec<'a>, Int -> 'a answers whatever the Vec holds.

Modules

Primitive types
Int, Float, Bool, String, (), and the unnamed shapes built from them — records and tuples.
Bytes always in scope
A sequence of raw bytes, written b"…" — what a socket or a file carries before anyone has decided it is text.
Channel always in scope
A queue between fibres. Channel.send puts a value in without waiting; Channel.recv takes the oldest one out, waiting until there is one. How forks share data.
Env use "std/env.nib"
Configuration by name: Env.Vars reads the process environment, Env.Cfg reads values an operator supplied on purpose. Both answer by get, and Env.Process answers both.
Failure
A raise held as a value — what a rescue f => arm binds — typed by the tags it can be, opened by match, and raised again with raise f.
Fiber always in scope
A handle to a task started with fork inside a Switch.run scope. Fiber.await waits for it to finish.
Float always in scope
Crossing between Float and Int, and the rounding and roots an operator cannot spell. The arithmetic itself is the dotted operators +. -. *. /..
Handle always in scope
A unique identity minted at run time with Handle.make. It has no literal and no conversion from any other type, so it cannot be guessed or forged.
Http use "std/http.nib"
HTTP/1.1 requests and responses as values: Http.parse turns bytes into a Request, Http.text/html/json build a Response, and Http.render turns it back into bytes.
Io use "std/io.nib"
The terminal and the simplest file read: Io.Print writes text as the program runs, Io.File reads a whole file.
Io.Fs use "std/fs.nib"
Reading and writing files, as two interfaces — Io.Fs.Read looks, Io.Fs.Write changes — both answered by Io.Disk.
Iter use "std/iter.nib"
Lazy walks over a sequence: build one from a Vec, a range or an endless producer, transform it element by element, and stop as early as the answer allows.
Json always in scope
A parsed JSON document. Json.parse reads one from text, the accessors take it apart, and Json.render turns any nib value into JSON text.
Map always in scope
A lookup table from keys to values, written {"a": 1, "b": 2}. Keys are kept in sorted order, and every operation answers a new Map.
Map.Opt use "std/mapopt.nib"
Map.Opt.get — look up a key and answer an Option instead of raising when it is missing.
Net use "std/net.nib"
TCP sockets: listen, accept, connect, send and receive bytes, through one interface, Net.Socket, answered by Net.Native.
Option use "std/option.nib"
A value that may be absent: Option.Some(x) or Option.None, with the helpers that read one without a match.
Random use "std/random.nib"
Random integers: Random.Source.next_int(bound) draws one from 0 up to bound. Random.System is the real source; a reproducible one is a few lines of your own.
Record always in scope
A record like another with some fields replaced, written #{ r with field: value }.
Route use "std/route.nib"
A route table as a value: match a request's method and path against patterns like /notes/:id, and read query strings and form bodies. Nothing here raises.
Sched use "std/sched.nib"
The schedule: Sched.Policy.pick decides which ready fibre runs next. Bind it in a wire to an effector of your own; leave it unbound for the runtime's FIFO queue.
Secret always in scope
A credential a program can use but never print or render, and which a journal records by label only. Secret.reveal is the one way the value comes out, spelled where a reader can see it.
Server use "std/server.nib"
A web server in one call: Server.run(host, port, handler) binds, accepts, speaks HTTP/1.1 with keep-alive, and answers each request with your handler.
Set always in scope
An unordered collection of distinct values, written #{1, 2, 3}. Members are kept in sorted order, and every operation answers a new Set.
String always in scope
Immutable text, written "…", with {expr} interpolation and ++ to join. Lengths and offsets count bytes.
Switch always in scope
A scope for concurrent fibres. Switch.run opens one, fork starts fibres in it, and the scope does not end until every fibre it started has finished.
Symbol always in scope
0.2's name for Handle, still accepted through 0.3 so a 0.2 program compiles; write Handle in new code.
System use "std/system.nib"
System.Remote, an instance whose operations are answered by another process over a socket. A wire binds it to an interface of yours, and that interface becomes the protocol.
Time use "std/time.nib"
The clock: Time.Clock.now() reads wall-clock milliseconds, sleep(ms) waits. Time.System is the real one; a test writes its own.
Vec always in scope
An ordered sequence of values of one type, written #[1, 2, 3]. Every operation answers a new Vec; none changes the one it was given.
Vec.Opt use "std/vecopt.nib"
The Vec reads that can miss — get, first, last, find_index — answering an Option instead of raising or answering -1.
World use "std/world.nib"
The simulated world a wire runs in: its network, schedule, crashes and seed, as a typed value a wire names with in.