Modules¶
Importing a file¶
Use import to run another .luz file. Everything defined in that file (variables, functions, classes) becomes available in the global scope:
Path resolution¶
The path in import is relative to the file that contains the import statement.
Execution model¶
When a file is imported:
- The file is read and executed from top to bottom.
- All definitions land in the global scope of the importing file.
- The import happens at the point where the
importstatement appears.
Circular imports¶
Luz automatically tracks which files have been imported. If a file tries to import a file that is already being executed (directly or transitively), the second import is silently skipped.
Selective import¶
Use from "file" import name1, name2 to import only specific names into the current scope:
If any of the requested names does not exist in the module, an ImportFault is raised and nothing is imported — it's all-or-nothing.
Module alias¶
Use import "file" as alias to wrap the module's exports in a namespace object. This avoids polluting the global scope:
Access any exported name with dot syntax: alias.name.
Example¶
greetings.luz:
function hello(name) {
return $"Hello, {name}!"
}
function goodbye(name) {
return $"Goodbye, {name}!"
}
main.luz: