Modules and imports

A program can be several files. imports: in the frontmatter names the other .md files, and each one's declarations are reached through its module: name.

---
midad: "0.1"
module: ledger
imports:
  - ./lib/money.md
  - ./lib/format.md as fmt
---

Paths are relative to the importing file and must end in .md.

What an import contributes

An imported file contributes its declarations:

  • functions (fn blocks)
  • state blocks
  • types blocks
  • native: bridges
  • ai:intent directives
  • html: templates

It never contributes its exec block, its routes, its commands or its before blocks. Importing a library cannot run code or serve a route. Each of those blocks in an imported file is reported rather than dropped silently, because a file that looks like it runs and quietly does not is the worst outcome:

block in an imported file code
exec E0315
route: E0316
command: E0317
before E0319

Permissions do not flow either. The importing program's frontmatter is the only grant, and imported code is checked against it. A library that reads a file is legal only where the program that imports it granted that path, and a library cannot widen the program that uses it.

Names and aliases

Each file's module: is the name its declarations are reached through: money.total(...), store.count, html("view.page"). An import line can ask for another name with as, which is what you do when two libraries would otherwise collide:

imports:
  - ./lib/money.md
  - ./lib/money_v2.md as cash
  • Two files may declare a function, state or template with the same name: money.total and cash.total are different names and neither is in the way.
  • Two imports may not take the same alias (E0312).
  • An alias may not be a name a builtin already uses, such as int or time (E0318).
  • A name declared twice inside one file is E0200.

Inside its own file a module writes its declarations unqualified: in money.md, total calls cents(...) as cents(...) and reads its own state the same way. Qualifying is for the other side of the module boundary.

A worked example

lib/money.md
---
midad: "0.1"
module: money
---

# money

```fn
function total(cents: list): number {
  let sum = 0;
  for c in cents {
    sum = sum + int(c);
  }
  return sum;
}

function format(cents: number): string {
  return str(idiv(cents, 100)) + "." + str(cents % 100);
}
```
lib/tax.md
---
midad: "0.1"
module: tax
---

# tax

```fn
function vat(cents: number): number {
  return idiv(cents * 14, 100);
}
```
main.md
---
midad: "0.1"
module: ledger
imports:
  - ./lib/money.md
  - ./lib/tax.md as vat
---

# ledger

```exec
const prices = [1250, 399, 25];
const net = money.total(prices);
print(money.format(net));
print(money.format(net + vat.vat(net)));
```
midad check .
ok

How the merge works

The files are merged into one program before the checker runs. Each module's declarations are renamed to alias__name, and every reference to them, in every file, is rewritten to the same name. Only name strings change: spans still point at the source bytes, so a diagnostic shows the file, line and column the user wrote, with the name they wrote (money.total, never money__total).

Nothing downstream of the merge knows that modules exist, which is why the interpreter and both native backends did not need a single change to support them.

Two consequences worth knowing:

  • A file reached twice is loaded once, so diamond imports are fine.
  • persist: writes state by its merged name, so an imported module's state is stored as module__name. The entry file's own state keeps its name.

The errors

code what it means
E0310 import cycle; the message names the chain
E0311 the file cannot be read or does not exist
E0312 two imported files take the same alias
E0313 an import that is not a .md file
E0314 an imported file that is not a midad program
E0318 an alias that is not a name, or one a builtin already uses

A cycle is caught before anything runs:

a.md
---
midad: "0.1"
module: a
imports:
  - ./main.md
---

# a

```fn
function one(): number {
  return 1;
}
```
main.md
---
midad: "0.1"
module: main
imports:
  - ./a.md
---

# main

```exec
print(str(a.one()));
```
midad check
× E0310: import cycle: main.md -> a.md -> main.md

Vendoring a library

There is no package manager. A library is a directory of .md files that import each other, and using it means copying it into the project with midad vendor, which also writes a fingerprint so midad check can tell you when the copy has drifted (W003). A project walks through it, and midad vendor has the flags.

Where a module belongs

lib/ is for your own code, routes/ for handlers, vendor/ for copies of other people's libraries and tests/ for programs that check the project. An imported module outside those folders is a warning (W007), not an error: it still checks and runs, and the warning says where it belongs.

On this page