PeaBox Lab › PeaLang › Learn › Chapter 8 › Lesson 1

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:

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:

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:

let payload = std.json.parse(text)

std is a built-in namespace: it is available without an import. Project modules use a quoted path and an optional alias, for example import "pricing/rules.pea" as rules. The exact 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

SurfaceExpected useEngineering response
Stable public APIProduction dependenciesUse documented contracts and test upgrades.
Preview or evolving APIEvaluation and controlled adoptionIsolate behind an adapter, pin the toolchain, and expect change.
Internal or undocumented APIRuntime implementationDo 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:

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:

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

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

MistakeConsequenceCorrection
Guessing a module or function nameImport or member-not-found failuresInspect documentation from the installed release.
Using undocumented membersSilent breakage on upgradeRestrict dependencies to documented public exports.
Broad wildcard importsName collisions and hidden ownershipImport a module or a small explicit member set.
Assuming import grants permissionCapability denial at runtimeDeclare least privilege and handle denial explicitly.
Testing only successful callsUnstable operational behaviorTest invalid input, denied capability, budget exhaustion, timeout, and cancellation.
Comparing incidental message textBrittle testsAssert documented categories, types, and stable fields.
Upgrading source without the runtimeContract mismatchUpgrade and verify the complete toolchain together.

1.10 Diagnostic workflow

1.11 Guided practice

For each scenario, identify the dependency, the evidence you need, and the safest design.

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

Deliverables

Acceptance criteria

1.13 Review questions

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.

← Previous Next →