use statements
use brings a name from another namespace into the current file’s
scope, so you can reference it by bare name instead of by fully
qualified path.
Two forms
Single:
use net::limits::Port
Port HTTP_PORT = 8080
Brace group:
use net::limits::{Port, IP, CIDR}
Port HTTP_PORT = 8080
IP FALLBACK = "10.0.0.1"
Both forms are equivalent for resolution; brace is just shorthand for
multiple use lines from the same path.
Placement
use statements appear after the optional namespace line and before
any other declarations:
namespace api::v2 // optional override
use net::limits::{Port, IP}
use core::types::LogLevel
Port HTTP_PORT = 8080
LogLevel DEFAULT_LEVEL = Info
Out-of-order use statements (e.g. interleaved with constants) are a
parse error.
Resolution
When you write a bare type name like Port in a file with
use net::limits::Port, primate resolves it as if you’d written
net::limits::Port. The resolution order in a file is:
- The current file.
- Sibling files in the same namespace.
- Names brought into scope by
usestatements. - (Otherwise:
unknown-typeerror. Use a fully qualified path.)
Diagnostics
| Code | Triggered when |
|---|---|
unresolved-import | use a::b::C where a::b::C doesn’t exist. |
import-collision | A use brings in a name that collides with a same-namespace declaration, or with another use. |
Formatter behavior
primate fmt normalizes the use block at the top of the file. The
rules:
- A single-item brace group collapses:
use a::b::{X}→use a::b::X. - Multiple
usestatements with the same path merge:use a::b::X+use a::b::{Y, Z}→use a::b::{X, Y, Z}. - Top-level
uselines sort lexicographically by path. - Items inside a brace group sort lexicographically.
Example:
// Before
use core::types::LogLevel
use net::limits::{Port}
use net::limits::{IP, CIDR}
use core::types::Status
// After `primate fmt`
use core::types::{LogLevel, Status}
use net::limits::{CIDR, IP, Port}
A /// or // comment immediately above a use line pins it: sort
and merge happen within contiguous comment-free runs only. This avoids
silently moving a comment away from the line it annotates.
Effect on generated code
use is a source-only ergonomic. Generators don’t see imports — they
work off fully resolved IR types. So use net::Port followed by
Port HTTP_PORT = 8080 generates the same output as
net::Port HTTP_PORT = 8080.