Imports and auto-imports
Imports and auto-imports
Six modules are imported into every source file automatically. Two of them are unpacked, so their names are used bare; the other four are bound to a prefix.
| Module | Written as | Example |
|---|---|---|
std::core |
bare | string, number, Object, Option |
std::collections |
bare | Array, Map, StringBuilder |
std::runtime |
runtime:: |
runtime::create_managed |
std::json |
json:: |
json::parse |
std::xml |
xml:: |
xml::Element |
std::bundle |
bundle:: |
bundle::name |
Everything else needs an explicit import:
import std::fs; // then fs::read_file
import std::io as io; // an alias works tooA plain import binds the last path segment, so import std::fs; gives you fs.
An as clause overrides that.
Inside a package
A module that is part of a package gets a smaller prelude: only std::core and
std::collections unpacked, plus std::bundle. It does not get json,
xml, or runtime for free.
That is why library code carries imports that an application would not need:
import std::json;
import std::runtime;The three foundational modules (core, collections, runtime) get no prelude
at all, since they are what the prelude is made of.
Glob imports
import { * } from core; brings a module's public items into scope unqualified.
The standard library uses this internally; application code rarely needs it.