Modules (v0.2.2)
Imports, aliases, and selective import scopes.
Latest
Overview
Modules are brought into a file with import. A file can import a full module, give it an alias, or import only selected names.
Use selective imports when a file only needs a few symbols. This keeps the dependency list easier to scan.
Aliases are useful when a module path is long or repeated often.
Primary source: syntax/modules/import.md
Syntax Signatures
import std::io;
import std::collections::vector;
import "other.thrust" as dep;
import "other.thrust" only { stack, counter };
Behavior and Use
Imports should show what the file actually depends on. Avoid importing large modules when one or two names are enough.
Use a consistent import style across nearby files so readers do not have to adjust to a new pattern each time.
Import Forms
Imports control which external names are available in the current file.
- import std::io brings a module into scope through its module path.
- import "file.thrust" imports a source file by path.
- as gives an imported module or file a shorter local name.
- only limits the import to selected symbols. Use it when a file depends on a small part of another module.
Name Resolution
Qualified names make module boundaries explicit.
- Use module::symbol when a name comes from an imported module.
- Aliases reduce repeated long paths but should still describe the imported code.
- If two imports expose the same unqualified name, keep the access qualified instead of relying on reader memory.
Example
import std::io;
fn main() s32 @public {
io::print("module import works\n");
return 0;
}
import std::collections::vector as vec;
fn main() s32 @public {
var values := vec::newVector[s32]();
vec::push[s32](ref values, 10);
vec::push[s32](ref values, 20);
var total: s32 = vec::front[s32](ref values) + vec::back[s32](ref values);
vec::dropVector[s32](ref values);
return total;
}
import std::io only { print };
fn main() s32 @public {
print("selective import keeps this file small\n");
return 0;
}