Design a library's public surface with `private`
Goal: keep a commons’ helpers to itself, so the units that uses it see
only the functions and types you mean them to depend on.
Every declaration is public by default: a unit that uses a commons can call
each of its functions and name each of its types. Mark a helper private and
it stays inside its own unit. Suppose a pricing commons computes a basket’s
total, with a helper for a single line:
commons pricing
type Item = { price: Int, qty: Int,}
fn total(items: List[Item]) -> Int { items.fold(0, (acc, i) => acc + lineTotal(i))}
private fn lineTotal(i: Item) -> Int { i.price * i.qty}total is the surface. lineTotal is how it works, and you can change or
remove it without breaking anyone.
What private hides
Section titled “What private hides”A private declaration is visible throughout its own unit, across all of the
unit’s files, and to the unit’s suites. It is never visible through uses.
- Functions and types.
private fnandprivate typework as above. - Methods.
private fn Item.discounted(self)hides one method whileItemstays public. The methods of a private type are hidden with it. - Assets and embeds.
private assetandprivate embedhide the declaration’s name from importers. A private asset is still served by the platform:privateis about names, not about who can fetch the file. - Constants.
private const Step: Int = 5keeps a tuning value to the unit, so an importer can’t come to depend on it. An importer may declare, or import from another commons, a public constant of the same name. - Message bundles.
privateon a bundle’s@referenceblock keeps the bundle out of every context that uses the commons, so no context negotiates its locale against it; the commons’ ownrenderstill reads it. That is rarely what you want, so the compiler warns (bynk.visibility.private_messages), as it does forprivateon any other block, where it has no effect.
A unit that uses pricing does not see lineTotal at all, so it may
declare its own lineTotal without a name conflict. If it calls pricing’s,
the compiler says why the call fails (bynk.visibility.private_item) rather
than reporting an unknown function:
commons shop
uses pricing
fn cost(i: Item) -> Int { lineTotal(i) // ✗ `lineTotal` is a private function of `pricing`}Keep private types out of public signatures
Section titled “Keep private types out of public signatures”A public function, method, record field, variant payload or constant’s type
can’t mention a private type of its own unit, since an importer could neither name nor build a
value of it (bynk.visibility.private_in_public). Either make the type public,
or make the declaration that mentions it private too:
commons pricing
type Item = { price: Int, qty: Int,}
private type Tally = { lines: Int, sum: Int,}
private fn tally(items: List[Item]) -> Tally { Tally { lines: items.length(), sum: items.fold(0, (acc, i) => acc + i.price * i.qty), }}
fn summary(items: List[Item]) -> String { let t = tally(items) "\(t.lines) lines, \(t.sum) in total"}summary is public and returns a String, so Tally never escapes.
Test the helpers
Section titled “Test the helpers”A suite targeting the unit sees its private declarations, so you can test a helper directly:
suite pricing { case "a line's total" { expect lineTotal(Item { price: 3, qty: 4 }) == 12 }}The exception is for the suite’s own target. A commons that the target uses keeps its private declarations, in a suite as anywhere else.
A system suite goes further: it may name any type a participant context
declares, private ones included, though a context that consumes the
participant may not. A harness test often needs to build or inspect a value
the system keeps to itself.
Where private doesn’t apply
Section titled “Where private doesn’t apply”private modifies a fn, type, messages, asset, embed or const
declaration in a commons or a context. Services, agents, capabilities, providers, actors
and events can’t be private (bynk.visibility.private_not_allowed): a
context’s exports and consumes
govern how far they reach.
private is a contextual keyword. Only immediately before one of those
declarations is it the modifier, so @cache(scope: private) and a parameter
named private mean what they always did.