// util.trpub fn api(): int { return helper() + 1; }fn helper(): int { return 41; }// main.trimport util;fn main(): int { return util::helper(); // ERROR: I cannot use the function `helper`, which is private to // module `util`. // Note: declared in module `util` without `pub` // Hint: Mark `helper` as `pub` in util.tr with `pub fn helper(...)`, // if other modules are meant to use it. Otherwise, use one of the // public functions that `util` offers: api.}Everything a module declares at its top level – functions, structs, enums,
errors, machines, extern fns – is private to that module unless it is written
with pub (or public, the same word). Another module can only use the
pub ones. That keeps a module’s helpers free to change, and makes its
public surface something you can read off the file.
The error appears however the private name is reached:
util::helper() // a qualified callmut h: util::Hidden = ...; // a type annotationutil::Hidden::default() // constructing a private structutil::HiddenColor::A // a variant of a private enumimport util::helper; // asking for it by nameimport util::[api, helper]; // ...in a listMembers of a pub type are checked the same way – a struct’s fields and
methods are private to its module unless written pub, even when the struct
itself is pub:
// shapes.trpub struct Point { pub x: int, pub y: int, cache: int }pub fn Point.len(): int { ... }fn Point.refresh(): void { ... }// main.trp.x // ok: a `pub` fieldp.cache // ERROR: I cannot use the field `Point.cache`, which is private to module `shapes`.p.len() // ok: a `pub` methodp.refresh() // ERROR: I cannot use the method `Point.refresh`, which is private to module `shapes`.Point::default() // always fine -- it only zero-fills, so it cannot // break an invariant that zero cannotFor a private field the hint names the type’s public methods, because that is
usually how the data is meant to be reached: List.data is a raw pointer, so
l.data[0] says to use get, set, length and the rest instead.
Call syntax cannot get around it. p.secret_twice() resolves through
“uniform function call syntax” to a free function secret_twice(p) – and a
free function another module did not mark pub is not a candidate there.
A [*] import (import util::[*];) is different: it just skips the private
names, so it never raises this error, and a private name in util can never
clash with one of your own.
There are two ways to fix it, and the hint offers both:
- The module is yours and the name is meant to be shared: add
pubto the
declaration in the module's own file -- pub fn helper(): int { return 41; }- The name is an implementation detail of somebody else’s module: use what
the module does offer. The hint lists its `pub` functions or types.Two things that are NOT errors, because they never cross a module boundary:
a module using its own private names (api() above calling helper()), and
a pub generic – function or struct method – whose body uses a private
helper or a private field: when you instantiate it from your own file, the
copy is still checked as the module that wrote it.
pub immut constants are always public: export is their pub.
Run turf --explain E088 in a terminal to read this again.