Visibility — Public vs Private Symbols
Nitpick uses a simple, binary visibility model. A symbol is either public (accessible from outside its defining module) or private (accessible only from within its defining module).
The pub Keyword
To make a symbol public, prefix its declaration with the pub keyword.
pub func:hello = int32() { pass 0i32; };
pub struct Point { int32:x; int32:y; };
pub mod utils { ... }
Applicable Declarations
The pub keyword can be applied to the following declarations:
- func: (functions)
- struct
- enum
- Rules
- Type
- const (module-level constants)
- fixed (module-level immutable variables)
- mod (modules)
Default Visibility (Private)
If the pub keyword is omitted, the symbol defaults to private. Private symbols cannot be imported or accessed by any other module.
// Private by default
func:internal_helper = int32() { pass 0i32; };
// Cannot be accessed by other modules via `use "this_file.npk".*`
Intra-Module Access
Within the same module (including the same file), all symbols are accessible to each other, regardless of whether they are marked pub or not.
func:helper = int32() { pass 42i32; };
pub func:main_api = int32() {
// Calling a private function within the same module is completely valid.
int32:val = raw helper();
pass val;
};
Cross-Module Access
When another module imports your file via use, only symbols explicitly marked as pub are exported and made available to the importer.
// file: math.npk
pub func:add = int32(int32:a, int32:b) { pass (a + b); };
func:sub = int32(int32:a, int32:b) { pass (a - b); }; // private
// file: main.npk
use "math.npk".*;
func:main = int32() {
int32:x = raw add(1i32, 2i32); // OK
// int32:y = raw sub(2i32, 1i32); // Error: sub is private
exit 0;
};
Error Messages
If you attempt to access or selectively import a private symbol from another module, the compiler will emit an error:
Symbol 'sub' is private and cannot be used outside its defining module.
To resolve this, ensure that the symbol is marked pub in its defining file.
Related
- use_import.md — importing modules
- mod.md — defining modules