Skip to main content

Importing Modules

Top-level module Name; lines declare which other modules this compilation unit depends on. They are not Python-style “rename this file”; the file’s default logical name comes from the filename stem (e.g. Player.mx → module Player).

The resolver also scans these lines when building the module graph (extractModuleImports / ModuleResolver.cpp). Semantic analysis registers each as an import and introduces a namespace symbol (DeclarationAnalyzer::visitModuleDecl).


Syntax

module Math;
module System;
  • Place module …; (and expand module …; if you use expansion) before classes, methods, and other top-level declarations (CORE1103 otherwise).
  • You cannot list your own module: if enforceStrictFileNamingRules applies and the name matches the current file stem, the compiler reports CORE0401 (“cannot import itself”).
  • expand module Name; follows the expand-module rules for pulling in expanded surface area (see diagnostics / module docs for edge cases).

There is no separate import keyword in the surface grammar today; module X; is the dependency declaration.


Resolution order (typical project)

The module index is built from, among others:

  1. src/ (or package.source_dir from morph.toml) — one class per <Name>.mx, module id <Name>.
  2. modules/ — installed packages: their morph.toml source_dir is indexed the same way.
  3. Builtin / packaged library roots when the driver includes them (tool-dependent).

If module Foo; appears but Foo is not in the index, you get CORE0401 (“Unknown module”) with a hint to add the dependency (morph add … in current branding, not morphlang add).


Using symbols from another module

After module Math;, qualify callables and types with the module name, e.g. Math.sqrt(16.0), subject to visibility and registration rules.

module Math;

Init method() {
result is Math.sqrt(16.0);
Print(result);
}

(Assume this lives in e.g. Main.mx, not Math.mx.)


Self-import

A file must not depend on itself. Do not add module Player; at the top of Player.mx unless your tooling explicitly allows a different stem rule.


Real example

module System;
module Tensor;

Init method() {
t1 is CreateTensor();
t2 is CreateTensor();
Print("Done.");
}

Next steps