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
- Import cycles are errors.
module cycle: a imports b imports a. A dependency loop is a design smell wearing a build system as a disguise. - Modules load once. Two files importing
baseshare onebase— its structs are the same types, its state would be the same state, if Sure had global state, which it doesn't. - A module's own imports are its own. If
geometryimportstrig, you do not gettrigby importinggeometry. Import what you use, where you use it. - Module names are reserved. You cannot declare a variable
named
geometryin a file that imports it, and a module is not a value —let g = geometryis refused on principle.
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