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
Vecis named for the type its functions work on. - type — a primitive type such as
IntorBool: literals and operators, and no module of functions. - data — a record or a sum declared with
data; constructors are writtenOption.Some(1), matched asSome(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.Stdoutis installed bynibc; 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 afterraises.
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.sendputs a value in without waiting;Channel.recvtakes the oldest one out, waiting until there is one. How forks share data. - Env
use "std/env.nib" - Configuration by name:
Env.Varsreads the process environment,Env.Cfgreads values an operator supplied on purpose. Both answer byget, andEnv.Processanswers both. - Failure
- A raise held as a value — what a
rescue f =>arm binds — typed by the tags it can be, opened bymatch, and raised again withraise f. - Fiber always in scope
- A handle to a task started with
forkinside aSwitch.runscope.Fiber.awaitwaits for it to finish. - Float always in scope
- Crossing between
FloatandInt, 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.parseturns bytes into aRequest,Http.text/html/jsonbuild aResponse, andHttp.renderturns it back into bytes. - Io
use "std/io.nib" - The terminal and the simplest file read:
Io.Printwrites text as the program runs,Io.Filereads a whole file. - Io.Fs
use "std/fs.nib" - Reading and writing files, as two interfaces —
Io.Fs.Readlooks,Io.Fs.Writechanges — both answered byIo.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.parsereads one from text, the accessors take it apart, andJson.renderturns 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 newMap. - Map.Opt
use "std/mapopt.nib" Map.Opt.get— look up a key and answer anOptioninstead 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 byNet.Native. - Option
use "std/option.nib" - A value that may be absent:
Option.Some(x)orOption.None, with the helpers that read one without amatch. - Random
use "std/random.nib" - Random integers:
Random.Source.next_int(bound)draws one from0up tobound.Random.Systemis 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.pickdecides 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.revealis 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 newSet. - 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.runopens one,forkstarts 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; writeHandlein 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.Systemis 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 newVec; none changes the one it was given. - Vec.Opt
use "std/vecopt.nib" - The
Vecreads that can miss —get,first,last,find_index— answering anOptioninstead 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.