imports
This commit is contained in:
15
specs.md
15
specs.md
@@ -1588,7 +1588,7 @@ This works for any function, not just `format`. The mechanism is general: the VM
|
||||
|
||||
### `#import` Directive
|
||||
|
||||
The `#import` directive brings declarations from another `.sx` file or directory into the current file. Paths are resolved relative to the importing file's directory.
|
||||
The `#import` directive brings declarations from another `.sx` file or directory into the current file.
|
||||
|
||||
**Flat import** — splices all declarations from the imported file into the current scope:
|
||||
```sx
|
||||
@@ -1616,12 +1616,23 @@ std.print("hello");
|
||||
### Import Resolution
|
||||
|
||||
- Imports are resolved after parsing and before code generation.
|
||||
- Paths are relative to the directory of the file containing the `#import`.
|
||||
- Paths are first resolved relative to the directory of the file containing the `#import`. If not found, they fall back to the working directory (cwd). This allows modules in subdirectories to import shared modules using the same paths as the root file.
|
||||
- If the path resolves to a file, it is imported directly. If it resolves to a directory, all `.sx` files in that directory are aggregated.
|
||||
- Nested imports are supported (imported files may themselves contain `#import`).
|
||||
- Circular imports are detected and silently skipped (each file is imported at most once).
|
||||
- Generic functions in namespaced imports are supported (e.g., `std.mul(5, 2)` where `mul` is generic).
|
||||
|
||||
**Example:** Given this project layout:
|
||||
```
|
||||
project/
|
||||
modules/std.sx
|
||||
modules/math/
|
||||
math.sx
|
||||
vector3.sx ← contains: #import "modules/std.sx";
|
||||
main.sx ← contains: #import "modules/std.sx";
|
||||
```
|
||||
When compiling from `project/`, both `main.sx` and `modules/math/vector3.sx` can use `#import "modules/std.sx"` — the root file resolves it relative to its own directory, and the nested file falls back to resolving relative to cwd.
|
||||
|
||||
### Intra-module References
|
||||
|
||||
Functions within a namespaced import can call each other without the namespace prefix. When generating code for a namespaced module, unresolved function names are automatically tried with the namespace prefix.
|
||||
|
||||
Reference in New Issue
Block a user