Features · Contexts

Contexts

Some values belong to a whole region of a program rather than to a single function: the signed-in member, the current theme, an open connection, the node you are building into. A context makes such a value available by name to everything running within a scope, so you never have to thread it through every call in between.

A value in scope

Open a context with a name and a value, run some work, then close it. While it is open, any code below — however deep in the call tree — can ask for that name and get the value back, type-checked. Contexts nest: opening one with a name already in use saves the outer value and restores it afterwards, so an inner region can override what an outer one provided without disturbing it.

Each context is just an immutable entry on a stack, so capturing the whole set is cheap — which is part of what lets a running program be snapshot and resumed elsewhere.

The kinds of context

The standard library builds a small family of contexts on that one mechanism, each for a different shape of value: