9. Imports and namespaces

Split a model across files and keep independently-authored names from colliding.

After this lesson you can

- Import declarations from disk with a quoted path - Scope a file's declarations with namespace - Reference a namespaced type by its qualified path

Before you start: Functions and higher-order patterns

A quoted import "./types.wcl" pulls a file's declarations in from disk, resolved relative to the importing file; an angle-bracket import <wdoc.wcl> asks the host for a *system* module (that one line is how a document opts into the whole wdoc stdlib). Imported declarations behave as if local — they participate in validation and name resolution.

A namespace declaration — first item in the file — scopes everything under a dotted path, so a library's Product is really shop.Product and can't collide with yours. Reference it by qualified path, pull names in with use, or pick a block schema explicitly with the ns::kind qualifier.

§ 1Exercise: A two-file model

Move a type into its own namespaced file and use it from the main document.

bash
mkdir -p mod
cat > mod/types.wcl <<'WCL'
namespace shop

type Product { name: utf8  price: f64 }
WCL
cat > main.wcl <<'WCL'
import "./mod/types.wcl"

@document
type Doc {
  featured: shop.Product
}

featured = { name: "Kettle", price: 39.5 }
WCL
wcl check main.wcl
wcl eval main.wcl featured --json

Expected result

wcl check prints OK, and the eval prints the record as JSON — the bare record coerced to shop.Product across the file boundary.

Hint

Rename the field's type to just Product and check fails: the import brought the declaration in, but its name stays qualified under shop.