.nib std
nib 0.3.1-pre

Pre-release documentation. This describes nib 0.3.1-pre, which is on main and has no release yet: what it documents beyond 0.3.0 may change before it ships.

module use "std/io.nib"

Io

The terminal and the simplest file read: Io.Print writes text as the program runs, Io.File reads a whole file.

Io holds the two interfaces almost every program meets first. Io.Print writes text to the terminal while the program runs, and Io.File reads a file. A module never touches the terminal or the disk itself: it performs an operation, says so in its effect row, and the wire decides which effector answers. Bind Io.Stdout for real standard output and Io.Disk for the real filesystem.

let _ = @Io.Print.println("starting");
let _ = @Io.Print.print("no newline, ");
let _ = @Io.Print.println("then one");
"finished"
starting
no newline, then one
finished

Printing or returning

A program has two ways to put text in front of a person. The entry function's answer is printed once, when the run ends — that is the right place for a document, a report or a build's output, and it needs no interface at all. Io.Print is for text that must leave now: progress from a long job, a log line from a server whose loop never returns, or a diagnostic that must not become part of the answer. Printed text is written and flushed immediately and is never taken back.

Reading files

Io.File is the read-only half of the disk, for programs that only look. A program that also lists directories or writes files uses Io.Fs.Read and Io.Fs.Write from Io.Fs; Io.Disk answers all three.

Interfaces

interfaceFile
interface File where
  read(path: String) -> String raises FileNotFound

Reads a file. read(path) answers the file's whole contents as a String, and raises Err.FileNotFound — with the path in the message — when there is nothing to read there.

Bind it to Io.Disk for the real disk. Because the module only names the interface, a test can bind a fake instead, written in the program itself:

use "std/io.nib"

effector Fixture is Io.File where
  read(path) =
    if path == "greeting.txt" then "hello from a fixture"
    else raise Err.FileNotFound(path)

module Notes where
  func first_line :: path: String -> String ! Io.File raises Err.FileNotFound =
    @Io.File.read(path)

  func main :: -> String ! Io.File raises Err.FileNotFound =
    let found = Notes.first_line("greeting.txt");
    let missing = try Notes.first_line("nope.txt") rescue Err.FileNotFound(msg) => "missing: {msg}" end;
    found ++ " / " ++ missing

wire Test runs Notes.main where Io.File = Fixture
hello from a fixture / missing: nope.txt
interfacePrint
interface Print where
  print(s: String) -> ();
  println(s: String) -> ()

Writes text to the terminal as the program runs. print(s) writes exactly s; println(s) writes s and one newline. Both answer () and neither can fail.

Each write happens when the operation returns, independent of the program's final answer — which is what a server loop or any run that never finishes needs. Bind it to Io.Stdout.

let names = #["ada", "bo", "cy"];
let _ = @Io.Print.println("{Vec.size(names)} names");
Vec.size(names)
3 names
3

Effectors

effectorIo.Stdout builtin instance
effector Io.Stdout is Io.Print  -- installed by nibc
  print :: String -> ()
  println :: String -> ()

The process's standard output, installed by nibc. Every operation is flushed before it returns, so each line reaches the terminal as it is written, interleaved correctly with whatever else the program does. Bind it with Io.Print = Io.Stdout.

use "std/io.nib"

module Main where
  func main :: -> String ! Io.Print =
    let _ = @Io.Print.println("one");
    let _ = @Io.Print.println("two");
    "done"

wire W runs Main.main where Io.Print = Io.Stdout
one
two
done