Export
Nolang uses the @ keyword in the package root lib.no file to declare exported items. External packages can only access these exported symbols when importing via #.
Note:
@only exports symbols between Nolang packages and does not produce C ABI symbols. To let C / C++ / Rust / Go / Java / Python / C# call into Nolang, use the#{cabi='symbol'}annotation withno build --sharedinstead. See Calling Nolang From Host Languages.
Syntax
@ path[.func]
[alias]
path— Module path (must start with a package name, e.g.test/src/assert; without the.noextension)func— Optional. Function/constant/enum to export; omit it to export the whole modulealias— Optional alias used when importing externally; may contain/(e.g.test/assert)
Rules
- Paths must start with a package name; no-space local addresses such as
@/local/pathare no longer allowed (same rule as#imports). - Spaced slash means "current package":
@ /mod(a space between@and/) exportsmod.nofrom the package containinglib.no— not@ mod/mod, and not the no-space@/mod(rejected by the CLI). Likewise# /modimports a module from the current package.no fmtpreserves the spaced form. - Export statements can only be written in the package root
lib.nofile (the one exception is the next section, "Global callable aliases") - One export item per line
- Symbol export
@ path.func [alias]exports a function, constant or enum from the module - Module export
@ path [alias]exports the whole module. The alias is optional and defaults to the last segment ofpath, so@ test/src/assertis equivalent to@ test/src/assert assert. A multi-segment alias such as@ test/src/assert test/assertmakes the module importable as# test/assert - A module export registers only a module name, not a callable alias; it cannot bind a type to a module (types have no global-name layer)
- Structs and enums referenced by exported functions are automatically exported — no manual declaration needed
- If an exported function does not exist in the module, LSP will report an error
Example
; lib.no - package root export file
@ package-name/src/utils.greet a
@ package-name/src/utils.hello b
@ package-name/src/math.pi
@ /src/utils.hello h ; module inside the current package (same target as the line above)
; src/utils.no
; Define exported functions
greet = (name str) {
print('Hello, ' - name)
}
hello = () {
print('Hi')
}
Global callable aliases
The same declaration, written in std/global.no instead of a package's lib.no,
means something different: it does not filter exports, it declares a global second
name that can be called. The bare name <alias> simply is path's func —
exactly as if the source had written <path>.<func>(...).
#{fmt-skip}
; std/global.no — two spellings, same meaning
@ std/err.new err ; export-style
err = err.new ; assignment-style
#{fmt-skip}
e = err('disk full', code.io) ; same as e = err.new('disk full', code.io)
m = e.msg() ; 'disk full'
Rules:
- Only
std/global.nohas this meaning;@in alib.nois still an export filter - The two spellings are equivalent — the compiler normalises them to one declaration, and writing both at once does not conflict
- The assignment spelling is valid only in
global.no. In an ordinary programx = mod.fnis a variable holding a function value (usable as an indirect call), not a global alias - Its right-hand side must be a bare
<module>.<function>reference: not a call, not a literal, not a field read, not a typed binding. When the target is not a function the declaration simply has no effect, so constant declarations such asPI = math.PIare unaffected - The alias applies to bare-name calls only. Qualified calls are unaffected:
io.err(...)is stillio's stderr writer and is not captured by theerralias - A module calling its own same-named function is unaffected (self-module carve-out)
- The alias's parameter list comes from the target; the result type is determined at the call site by the target function
- The alias does not conflict with the builtin option's
errvariant: where an option type is expected (x = err('m')afterx ?i64, anerr ->match arm, a function returning?T),err(...)is still the option's err variant constructor (tag 2)
Importing Exported Symbols
External packages can only access exported items declared in lib.no when importing via #:
; Import alias a (corresponds to package-name.utils.greet)
# package-name.utils.greet a
; Or use the function name directly
# package-name.utils.greet
Deliberately unused imports: the _ alias
Some modules must be imported directly by the entry file for their top-level
bindings (constants / globals / struct definitions) to be generated; reading them
through an indirect dependency silently yields 0 or fails to compile (bug #85).
Such imports have no symbol used in the file, so no vet emits an unimpuse
hint. Add a _ alias to mark the import as intentionally kept for its side
effects: the import still takes effect, the hint disappears, and the module's
prefix stays its usual ShortName (_ is not a module alias).
# glossary/src/nav _ ; keep the import to generate top-level bindings
# glossary/src/drv ; actually used here, keep the bare import
code = drv.dispatch()
This only applies to module imports (# path _). A function import
# path.fn _ is the existing spelling for discarding that function binding.
LSP Support
- Go-to-definition: Click on an exported function name or alias in
lib.noto jump to its definition in the corresponding module file - Auto-completion: Automatically suggests file paths and function names when typing
@and a path - Error diagnostics: Shows an error when an exported function does not exist in the module