Chapter 6

Modules

A module is a file. A file is a module. Everything else about Sure's module system follows from refusing to complicate that.

Importing

import geometry loads geometry.sure from the same directory as the importing file — once, no matter how many files import it. Its functions and structs are then reached through its name:

// geometry.sure
struct Point {
    x: Float,
    y: Float,
}

func origin() -> Point {
    return Point { x: 0.0, y: 0.0 }
}
// main.sure
import geometry

func main() -> Int {
    let p: geometry.Point = geometry.origin()
    print(p.x)
    return 0
}

Qualified names are not a politeness — they are the rule. An imported function cannot be called bare; there is no way to make origin() mean geometry.origin(), and therefore no way to wonder which file a name came from. What C solves with headers, include guards, forward declarations, and hope, Sure solves by not offering the problem.

Directories and names

import math.geometry loads math/geometry.sure. The module is still referenced by its last name — geometry.origin() — because the directory is where it lives, not what it is. When two modules would go by the same name, rename one for your file with as:

import math.geometry
import screen.geometry as sgeo

func convert(p: geometry.Point) -> sgeo.Point {
    // both geometries, no ambiguity, no guessing
    return sgeo.from_world(p)
}

Without the as, that pair of imports is a compile error naming both files and telling you to pick. Sure will not guess which geometry you meant, because guessing is how C linkers work.

Rules with reasons

Errors know their file

When something is wrong inside an imported module, the diagnostic says where:

🫤 math/geometry.sure line 8: not sure about this
    'origin' returns Point, not Float
Modules and memory. Each imported function is analyzed for its memory schedule in the context of its own file, with its own comments. A well-documented library is, in a specific and technical sense, a well-compiled library. Chapter 7 explains why, and you should let it — choose your dependencies the way you choose your words.