Namespaces
A Maxon program is every .maxon file in a project directory, compiled together. There are no import
statements: a file sees its own declarations, the declarations other files make visible, and the standard
library. A program that is one file also sees the scriptlib packages
(The scriptlib directory).
Automatic Derivation
Section titled “Automatic Derivation”A file’s namespace is its directory path relative to the project root, joined with .:
| File | Namespace |
|---|---|
main.maxon |
(none) |
utils/helpers.maxon |
utils |
lib/fmt/integer.maxon |
lib.fmt |
Every file in one directory shares that directory’s namespace.
A scriptlib package’s namespace is scriptlib.<package>: scriptlib/text/Lines.maxon is in
scriptlib.text, and its subdirectories extend it the same way.
Visibility
Section titled “Visibility”A top-level declaration — a function, type, enum, union, interface, typealias, let or var — written with no
modifier is visible to every file in its declaring directory and in that directory’s subdirectories, and to
no other file. The root directory contains every file, so an unmarked declaration there is visible to the whole
program. A top-level name is declared once per directory, whatever its visibility: a second declaration of it in
one directory is E3006 (see Bare Names and Ambiguity). Two modifiers widen
visibility:
| Modifier | Visible to | Checked for unused declarations |
|---|---|---|
| (none) | files in the declaring directory and its subdirectories | yes (E3219, E3062) |
export |
every file | yes (E3092) |
public |
every file | no |
E3219 and E3092 are lint rules, reported by maxon lint, maxon spec-test
and the MCP check tool; maxon build compiles a program that holds an unused declaration. E3062 is
reported by every compile.
project/├── main.maxon # cannot call helper()└── feature/ ├── api.maxon # function helper() — declared here ├── routes.maxon # can call helper() └── internal/ └── cache.maxon # can call helper()The subtree is decided by directory, segment by segment: feature2/ is not inside feature/, and
feature.extra/ is a directory of its own. A file outside the subtree that names an unmarked declaration is
refused where it writes the name: E3008 (function 'helper' is not exported). Program code and the standard
library never see each other’s unmarked declarations, whatever directories they sit in.
The same modifiers apply to members inside a type, with the same directory rule. An unmarked field is visible
to every file in its type’s declaring directory subtree: a method of another type, a free function and an
extension written there all read and write it, and reading or writing it from any other directory is E3014
(field 'Box.v' is not exported). An unmarked method or static member is visible the same way, and calling it
from outside the subtree is E3008. A typealias declared inside a type body carries its own modifier the same
way: unmarked, naming it from outside the subtree — bare or as Holder.Inner — is E3008. A typealias inside
an interface has the interface’s visibility. At most one modifier may be written; combining two is E2001
('export' and 'public' cannot be combined).
A type hides its members. Where a type is not visible, nothing of it is: naming it is E3008. A file that
writes the type’s name, or a hidden alias of it, gets that one refusal at the written name. A value reached
without its name being written, such as one an exported function returns, is refused at its member use. The
members are its fields, methods, extension methods, accessors and statics, and the calls the language makes on
the author’s behalf: toString in an interpolation, == and <, the iteration a for performs, and a match
over an enum or union value. Inside a generic function’s copy, the interpolation, ==,
< and for over a value of a type parameter are judged for the caller that instantiated it instead. A value
held at an interface type that is visible answers that interface’s requirements. The standard library follows
the same rule: a library type or interface without public is hidden from a program.
A signature may not name a type less visible than the function itself. Whoever may call a function has to
be able to name what the call takes and gives back, so every type its parameters, its return type and its
throws clause name must carry at least the function’s own modifier. For this rule the three tiers are ordered
(none) < export < public: public outranks export, so a public function may not name an export type.
The check is structural — a generic instance’s base type and each of its type arguments, a tuple’s elements, and
a function typealias’s parameter and return types are all asked; a type parameter and a primitive name no
declaration and are asked nothing. A typealias the signature names is walked through, each name on its
right-hand side read as the alias’s own file means it, and a type body’s inner typealias is held to its own
modifier before its right-hand side is walked. A member is held to its own modifier, whatever its type’s, and an
interface’s members — its requirements’ types included — are held to the interface’s. A
service’s message is held to the narrower of its own modifier and its service type’s. An
export or public binding — a top-level let or a static let member — is held to the same rule for its
type, which is the type its initializer gives it, written or inferred, together with any alias the initializer
was spelled through (50 as Small, Tallies.create()). Every other surface a declaration exposes is held to
the same rule: an instance field at the narrower of its own modifier and its type’s (an unmarked field is asked
nothing), a union case’s payloads at the union’s tier, a typealias’s target, an implements clause and its
with arguments, a type’s, generic function’s or extension’s where clause, an interface’s extends clause,
and a struct-backed enum’s backing record. A service’s handle is its service’s tier.
Naming a narrower type is E3167 (exported field 'feature.Shelf.box' names unexported type 'Box' in its type); the fix is to raise the type to the surface’s
tier, or to narrow the surface.
An unmarked top-level function, let, type, enum, union or interface that nothing live in its
directory subtree names is E3219 (unused function: 'helper' is not exported and nothing live in its directory names it; delete it). A declaration is live when code that is not itself an unmarked top-level
declaration names it (an export or public declaration, main, a test, a method), or a live unmarked
declaration does, so declarations that only name each other, or a function that only calls itself, are all
reported. A name spelled in a branch of #if the target excludes counts as a use. An unmarked typealias
nothing in its subtree names is E3062. maxon lint reports E3219 once the program has no other error.
export
Section titled “export”export typealias Score = int(i64.min to i64.max)
export function publicAdd(a Score, b Score) returns Score return a + bend 'publicAdd'
function privateHelper(x Score) returns Score // visible to this directory's subtree return x * 2end 'privateHelper'Calling privateHelper from a file outside the declaring directory’s subtree is E3008 (function 'privateHelper' is not exported).
export also states an expectation: this program uses the declaration from outside its directory. When
nothing outside the declaring directory’s subtree refers to it, maxon lint
reports E3092 (exported function 'geometry.perimeter' is never referenced outside its declaring directory): an unmarked declaration already serves every reader it has. The rule includes the root
directory, so in a program whose files all sit in one directory every export is reported. maxon lint
runs these checks on every program that otherwise compiles, a one-file program included. The entry point, every target a .maxproj file declares and every task a
.maxtasks file declares are exempt, because the driver calls them by name. A type an exported surface names —
a signature, a binding, a field, a payload, a typealias’s target or a clause — is exempt while that declaration
is itself referenced from outside its directory, or while the type owning the surface is itself exempt at that
tier: the surface requires the wider tier, so dropping the modifier would only trade E3092 for
E3167. Every typealias form — ranged, function,
generic-instance and tuple — is audited, and a use credits exactly the declaration it means, a use reached only
through a called function’s signature included.
public
Section titled “public”public gives exactly the visibility of export and declares the symbol API surface: something that
exists for callers outside this program, so “nothing here uses it” is not a finding. Library code marks its
surface public; the standard library does so throughout.
public typealias Length = int(0 to 1000)
public function area(width Length, height Length) returns Length return width * heightend 'area'export and public are reserved words.
Qualified Names
Section titled “Qualified Names”Qualify a name with its namespace to be explicit, or to choose between two declarations with the same
name. With geometry/area.maxon:
public typealias Length = int(0 to 1000)
public function area(width Length, height Length) returns Length return width * heightend 'area'main.maxon:
function main() returns ExitCode let a = geometry.area(3, height: 4) let side = 7 as geometry.Length print("{a} {side}\n") return 0end 'main'Qualification works for functions, for another directory’s top-level let and var (alpha.LIMIT), and
for every kind of type name — typealiases, types, enums, unions and
interfaces (lib.fmt.format(x), 50 as api.Score, api.Point.origin()) — at every position a type name
is written: a declaration, a cast, a construction, a static call’s base, a spawn’s target, an
extension’s target, a throws, implements, where or extends clause, and the head of a top-level
constant’s initializer. Visibility holds through it: a type the referring file may not name is refused
qualified exactly as it is bare.
A qualified name resolves the same way wherever it is written — an expression, a statement, a try, an
async call, a spawn or a default. A chain names a function (outer.inner.f()), a type and its member
(sub.Calc.make()), or a member type (lib.Type.Inner, the typealias Inner that lib’s Type
declares). When a binding in scope — a local, a parameter, or a top-level binding the file sees — has the
name of the chain’s head, the chain starts at that binding: with let util = 4 in the file, util.f() is a
method call on util. A top-level binding that shares its name with a type yields to the type where the
type declares the member written after it, a static or a static function. A directory may share its name
with a type or a primitive: beside a type Point, Point.helper() calls the directory Point’s helper
and Point.create() the type’s static, and byte.helper() calls byte/’s helper. Reading an
undeclared member of a directory is E2073. A call whose qualifier names
no binding, directory or type is E3004, and the
message names the qualifier: for foo.bar.baz(), nothing declares a binding, directory or type foo.bar.
Three qualifiers are reserved. export.X names a declaration at the project root, stdlib.X one in the
standard library, and scriptlib.<package>.X one in a scriptlib package. A source directory that cannot be
written as a qualifier is refused when the program is loaded, E3182: one whose name is not an
identifier, such as my-dir, and a top-level one named export, stdlib, runtime, scriptlib or a
keyword.
Bare Names and Ambiguity
Section titled “Bare Names and Ambiguity”A directory declares each top-level name once, in one namespace for every kind — a type, enum, union,
interface, typealias, top-level let or var, and function — whether the declaration is unmarked,
export or public. A second declaration of the name in the same directory is
E3006, in one file or across the files of the
directory (duplicate definition of 'Point' — already declared as type Point``), and a typealias against a
typealias is E3061. The declaration kept is the earlier one in a file, and across files the one in the
file whose absolute path, with / separators, sorts first byte by byte; each other one is reported at its
name. Directories are separate scopes, so a name two directories declare is two names. Locals and parameters
keep block scope and may shadow a top-level name.
A bare name resolves the same way for every kind of declaration, and the first step that has a declaration decides:
- the referring file’s own directory’s declaration;
- else the project root’s;
- else the one declaration the file may name;
- else it is ambiguous: E3063 for a type name and E3095 for a binding or a function, each listing the
spellings that resolve it (
export.Namefor a declaration at the project root).
Only a declaration the referring file may name is a candidate: an unmarked declaration counts only inside its directory’s subtree, and the candidate list an error prints names only visible ones. A bare call or a bare function value takes the type of the declaration it resolves to, and a function-backed enum case’s function is resolved from the file that declares the enum, whichever file reads the case; a case that resolves to no single declaration is reported in that file.
A type name — a typealias of any form, a type, an enum, a union or an interface — resolves by that order, so a file reads its own directory’s declaration bare and reaches another directory’s through the directory:
error E3063: app/main.maxon:7:11: Ambiguous type name 'Score': more than one visible declaration matches it. Qualify it as one of: api.Score, lib.ScoreA function name or a top-level binding resolves by the same order, and a bare call, function value or
read that reaches several declarations none nearer is E3095 in every form it takes — plain, under try,
or spawned with async (Ambiguous bare-name call to 'describe': more than one visible declaration matches it. Qualify it as one of: alpha.describe, beta.describe) — worded for a function value, an enum case’s backing or
a read where the name is one. A call (api.format(...)), a function value (let f = api.format), a
function-backed enum case (plain = api.format) and a read of another directory’s top-level let or var
(alpha.LIMIT) all accept the qualified form. A bare read of a name the reader’s directory declares as a
function means that function, even where another directory exports a let of it; dir.name reads the let.
A generic function resolves by the same order.
A program’s own declarations shadow the standard library’s and the runtime tier’s: they are different scopes. A
project declaration of a library name is what the bare name means in every project file, so a project’s
export typealias StringArray is the one a bare StringArray names, and the library’s stays reachable as
stdlib.StringArray. The standard library’s own files see only the library’s declarations. The same holds
for the protocols the library defines — Stringable, FormattedStringable, Hashable, Equatable,
Comparable, Cloneable and Error: a project’s own interface Comparable is what implements Comparable
names in project files, while the library’s generics, such as sort and contains, keep their library
constraints. int, float, bool, Character, String, arrays and payload-free enums meet the library’s
protocols. A b"…" literal’s element is the type the file’s bare Byte names when that is an integer alias —
the file’s own, else the one project alias it sees, else the library’s; a type Byte leaves the literal on the
library’s, and an ambiguous bare Byte refuses the literal with E3063.
Source comes in three layers, lowest first: the standard library with the
runtime tier, the scriptlib packages, and the program. A file
sees the members its own layer and the layers below it declare. A method, a static method or a nested
typealias that a program’s extension adds to a library type is therefore the program’s alone: a library
file that spells the same name, as a bare call to one of its own functions or as a member, keeps the
library’s declaration, and so "abc".contains("b") answers the same whatever the program’s
extension String declares. A package’s extension members stand the same way toward the library. A member
declared in a higher layer is reached from a lower one through a type that layer declares: the library’s
generics call the hash, equals, compare, toString and clone a program’s own type declares. A member
the compiler supplies to a type, such as the hash and equals it grants a payload-free enum
(Enum Interface Conformance), the clone of a struct, the toString and
clone of a primitive, or int.fromString, is declared only by the layer that declares the type: an
extension of that type in a layer above it that declares one is E3006 at the member’s name, whatever its
signature. Rename the method, or wrap the type in a type of your own that declares it. An extension of an
interface declares a default, which yields to a member the compiler supplies to a
conformer, whatever layer the conformer is declared in (see Interface Extensions).
Only a declaration the file can see counts, for every kind of name. A project type named like a
primitive (byte, say) and declared where a file cannot see it leaves that name the primitive in that file.
What a call knows about its callee comes from the declaration it binds, too: whether the callee never
returns (its body ends in panic) or can only throw, and which generic template it instantiates. A function
value (let f = helper) is the one declaration of its name the file can see, with that declaration’s
parameters and return type, whatever other files declare under the name.
Every typealias a public standard-library signature names is itself public — an inner one such as
Array.ElementIterator included — so a value can always be cast to the alias a library signature asks for
(x as ElementIndex). A standard-library typealias with no modifier
is hidden from every program — Math.maxon’s SeriesTermLimit is one — and naming it is E3008.
Multi-Project Workspaces
Section titled “Multi-Project Workspaces”Several projects can share a workspace. Each is a directory marked by its own .maxproj file, and the
directory you build is the one that is compiled:
workspace/├── project-a/│ ├── project-a.maxproj # how project A is built│ └── main.maxon└── project-b/ ├── project-b.maxproj # how project B is built └── main.maxonThe projects sit side by side: a .maxproj file inside another project’s tree is E2074. See
Build System and Project Structure for what a
project directory contains.
The language server checks open files. It checks a document together with the standard library, and reads the rest of the project to learn which names its other files declare, so it reports what the build reports about the names a buffer uses. A diagnostic that only the merged program raises is the build’s to report, and while a buffer is unsaved the name-dependent diagnostics are withheld until it matches disk again. The build remains the authority.