Part II — Intermediate Development · Chapter 8 · Lesson 1 of 6 · 8 min read
Lesson 1: Stable std Namespace and Compatibility Conventions
Level: Intermediate
Purpose. Learn to select, import, inspect, and upgrade standard-library modules without accidentally depending on unstable behavior, unavailable capabilities, or host-specific details.
Learning objectives
By the end of this lesson, you will be able to:
- Explain why PeaLang groups supported library functionality beneath the std namespace.
- Discover modules and inspect their public contracts before using them.
- Distinguish stable interfaces from preview or evolving surfaces.
- Use qualified names and imports that make dependencies easy to audit.
- Assess compatibility, deprecation, portability, determinism, and capability requirements.
- Diagnose common namespace, version, and permission failures.
1.1 The standard library as a supported contract
The standard library is the language platform’s shared toolbox. Its modules provide reusable operations for collections, text, mathematics, dates and times, data interchange, files, networking, cryptography, logging, testing, and concurrency. Placing these APIs beneath std separates platform-supported functionality from application modules and third-party packages.
A standard-library name is more than a convenient shortcut. It is a contract among source code, the language version, the runtime, the host, and the granted capabilities. Dependable programs therefore treat every library call as a visible dependency with a known contract and boundary.
Compatibility principle. Depending on a stable public contract is safer than depending on implementation details, incidental output formatting, undocumented members, or host objects.
1.2 Discover before you depend
Do not guess a module name or member signature. Start with the documentation and tooling shipped for the same PeaLang release as the runtime you will execute. Confirm the fully qualified module name, exported members, parameter and return contracts, failure behavior, capability requirements, and any stability notice.
A disciplined discovery sequence is:
- Identify the task, such as parsing JSON or calculating a date interval.
- Locate the corresponding std module in the release documentation.
- Inspect its public exports and examples.
- Check types, exceptional cases, resource limits, and required permissions.
- Write the smallest focused experiment.
- Run formatting, source checks, semantic checks, and tests as separate gates.
Tooling output and generated documentation should be treated as release-specific evidence. If documentation and runtime behavior disagree, stop and verify the installed toolchain rather than coding around the mismatch.
1.3 Qualified names and imports
Qualified names make ownership explicit. A reference that begins with std tells reviewers that the dependency belongs to the supported library surface rather than the current project or an external package.
Illustrative pattern:
import std.json let payload = std.json.parse(text)
The exact import form and member names must match the installed release. The important design choice is to preserve enough qualification that a reader can identify the providing module without searching the entire project.
Prefer module-level imports when several related members are used. Prefer a narrowly imported member only when it remains unambiguous. Avoid wildcard-style imports in shared modules because they hide origins, increase collision risk, and make upgrades harder to review.
1.4 Stable, preview, and internal surfaces
| Surface | Expected use | Engineering response |
|---|---|---|
| Stable public API | Production dependencies | Use documented contracts and test upgrades. |
| Preview or evolving API | Evaluation and controlled adoption | Isolate behind an adapter, pin the toolchain, and expect change. |
| Internal or undocumented API | Runtime implementation | Do not depend on it. |
Stability applies to the documented contract, not every incidental detail. Even a stable function may leave ordering, wording, timing, or representation unspecified. Tests should assert promised behavior rather than snapshots of accidental behavior.
1.5 Compatibility across releases
A program is compatible only when its source expectations, semantic rules, standard-library contracts, runtime behavior, and host capabilities agree. Record the intended PeaLang release in project configuration, use the project’s deterministic dependency and lock workflow, and test with the same toolchain family used in deployment.
Before upgrading:
- Read the release and migration notes for changed, deprecated, or removed library contracts.
- Regenerate or inspect module documentation for the target release.
- Run focused tests around each affected boundary.
- Run project-wide semantic analysis to catch shared-contract changes.
- Exercise both success and failure paths in the intended host environment.
- Update the version constraint only after the evidence is recorded.
Deprecation is a migration signal, not an immediate failure. Remove deprecated use deliberately: identify the replacement, compare semantics, adapt tests, migrate callers, and only then delete the compatibility path. Do not silence a warning without understanding what future incompatibility it predicts.
1.6 Deterministic and portable library use
Portable values—null, booleans, numbers, strings, lists, and string-keyed maps—make boundaries easier to test and move between hosts. Convert host-specific objects at the edge instead of allowing them to spread through business logic.
Determinism also requires control of environmental inputs. Time zones, clocks, locale, random seeds, filesystem order, network responses, and concurrency schedules can change outcomes. Pass such dependencies through explicit adapters, normalize outputs, and test with controlled substitutes.
Example design: a reporting function accepts already parsed rows and a clock value. A thin adapter uses the relevant std modules to read data and obtain time. The core function remains deterministic and capability-free.
1.7 Capability-aware usage
Some modules are computational; others cross security boundaries. File, HTTP, environment, cryptographic, and concurrency operations may require explicit capabilities and are still subject to runtime budgets. Importing a module does not grant authority to use it.
Apply least privilege:
- request only the capability needed for the current operation;
- validate paths, URLs, sizes, and timeouts at the boundary;
- bound input, output, retries, and concurrency;
- redact secrets and sensitive values from logs and errors;
- convert low-level failures into stable application outcomes;
- release resources exactly once on every terminal path.
This separation improves compatibility: pure code depends on portable contracts, while a small adapter owns host permissions and operational variability.
1.8 Annotated patterns
Pattern A: keep the module visible
import std.json function decode_request(text): return std.json.parse(text)
Why it helps. The call site exposes the provider, makes collisions unlikely, and gives reviewers a clear boundary to test. The parser’s documented failure behavior must still be handled by the caller.
Pattern B: isolate a changing surface
function parse_document(text): // The adapter is the only place that calls the evolving API. return preview_parser.parse(text)
Why it helps. If the preview contract changes, migration is concentrated in one adapter. The application-facing function should return stable portable values or a typed application result.
Pattern C: make environmental behavior explicit
function build_summary(rows, now): // Pure transformation: no hidden clock, file, or network access. return summarize(rows, now)
Why it helps. Passing time explicitly avoids tests that depend on the machine clock or time zone. An outer adapter can obtain the value through the documented date-time API.
1.9 Common mistakes and corrections
| Mistake | Consequence | Correction |
|---|---|---|
| Guessing a module or function name | Import or member-not-found failures | Inspect documentation from the installed release. |
| Using undocumented members | Silent breakage on upgrade | Restrict dependencies to documented public exports. |
| Broad wildcard imports | Name collisions and hidden ownership | Import a module or a small explicit member set. |
| Assuming import grants permission | Capability denial at runtime | Declare least privilege and handle denial explicitly. |
| Testing only successful calls | Unstable operational behavior | Test invalid input, denied capability, budget exhaustion, timeout, and cancellation. |
| Comparing incidental message text | Brittle tests | Assert documented categories, types, and stable fields. |
| Upgrading source without the runtime | Contract mismatch | Upgrade and verify the complete toolchain together. |
1.10 Diagnostic workflow
- Namespace failure: confirm spelling, qualification, and module availability.
- Member failure: inspect the release-specific exports and signature.
- Semantic failure: compare argument, return, generic, and protocol contracts.
- Capability failure: verify the manifest, runtime policy, and requested resource.
- Compatibility failure: compare toolchain versions and migration notes.
- Behavioral mismatch: reduce the issue to a small deterministic test before changing application code.
1.11 Guided practice
For each scenario, identify the dependency, the evidence you need, and the safest design.
- A formatter needs JSON parsing in three modules. Decide whether to qualify calls through the module or import individual members, and justify the choice.
- A preview date-time helper produces a convenient display value. Design an adapter so the rest of the project does not depend on the preview contract.
- A file utility works locally but fails in a sandbox. List the checks needed to distinguish a path error, capability denial, and resource-limit failure.
- A test compares the complete wording of an exception. Rewrite its assertion around the documented failure contract.
- A runtime upgrade changes output order that the documentation never guaranteed. Decide whether to sort explicitly or treat order as significant, and record the reason.
1.12 Hands-on lab: Standard-library compatibility audit
Scenario. A small command-line utility reads a configuration value, parses a portable document, normalizes selected fields, and emits a deterministic summary. The current implementation uses broad imports, undocumented behavior, ambient time, and unbounded input.
Your task. Refactor the utility into a pure core plus narrow standard-library adapters.
Required steps
- Create an inventory of every std module and member the utility requires.
- Record each member’s documented signature, failure behavior, stability status, and capability needs for the target release.
- Replace broad imports with explicit module ownership.
- Move file, time, and environment access into boundary adapters.
- Convert boundary values to portable data before calling the core.
- Add limits for input size, output size, and operation duration.
- Represent expected absence and recoverable failure explicitly.
- Add deterministic tests for success, malformed input, missing data, permission denial, limit exhaustion, and an upgrade-induced contract mismatch.
- Run formatting, focused semantic checks, project-wide checks, and the test suite as separate gates.
Deliverables
- a dependency inventory with stability and capability notes;
- the refactored adapters and pure core;
- a compact compatibility matrix for the current and target releases;
- tests covering every required outcome;
- a short migration note explaining any deprecated or preview usage.
Acceptance criteria
- No application module relies on an undocumented standard-library member.
- Every capability-requiring call is isolated, bounded, and auditable.
- The core accepts and returns portable values.
- Repeated tests produce the same result under controlled inputs.
- A denied capability and an invalid document produce distinct stable outcomes.
- The target toolchain passes project-wide semantic analysis and tests.
1.13 Review questions
- What does the std prefix communicate about a dependency?
- Why must module discovery use documentation from the installed release?
- When is a module-level import clearer than importing individual members?
- Why are wildcard-style imports risky in shared code?
- How should preview functionality be isolated?
- What does a deprecation warning require you to investigate?
- Why are host objects converted at a standard-library boundary?
- Which environmental inputs commonly undermine determinism?
- Why does importing a module not grant its required capability?
- Which checks distinguish a namespace error from a capability error?
1.14 Applied challenge
Choose one existing PeaLang utility that uses standard-library functionality. Produce a compatibility brief that lists every module and member, its contract and stability level, its capability and resource requirements, and the tests that protect it. Replace one hidden environmental dependency with an explicit adapter, introduce one deliberate version or permission mismatch, capture the diagnostic, and restore the correct configuration.
Lesson summary
The std namespace identifies PeaLang’s supported standard-library surface. Reliable use begins with release-specific discovery, explicit dependency ownership, documented contracts, and a clear distinction among stable, preview, and internal APIs. Compatibility depends on aligned source, tooling, runtime, host, and capabilities. Deterministic programs isolate environmental effects, exchange portable values, apply least privilege and resource bounds, and test both successful and failing paths.
Transition to Lesson 2
The next lesson applies these conventions to everyday computation with standard-library support for collections, strings, mathematics, and date-time operations.
Lesson status: Ready for review.