Modules
Spice supports the linkage of several source files to one executable. To do so, you can use the import statement for tying in another source file to the current one.
Importing own source files¶
Here is an example for importing an own source file into the main source file. Imagine you have the utils.spice file, containing some util functions, which you want to use in your main source file main.spice.
Util functions file utils.spice:
| Spice | |
|---|---|
Main source file main.spice:
| Spice | |
|---|---|
In the example above, the imported file lives in the same directory as the main source file. If you have a nested folder structure
you can state the import like this: import "sub-folder/sub-sub-folder/utils" as fibonacci.
Such import paths are relative to the directory of the importing source file. Import paths must not contain parent-directory
(..) components, so a source file in a sub-folder cannot import a source file from a parent folder this way. For that, prefix
the import path with @/. Then it is relative to the directory of the main source file, no matter which source file contains
the import:
| Spice | |
|---|---|
You can also write the source file like this, using anonymous imports:
| Spice | |
|---|---|
In the case that two imported source files expose symbols with the same name, Spice does not offer the anonymous usage of both
symbols to prevent the usage of the wrong symbol. Then, you need to specify explicitly, which of the two symbols is meant.
Using the unqualified name results in an Ambiguous symbol error, which names the colliding imports:
| Spice | |
|---|---|
A symbol declared in a source file itself hides imported symbols with the same name. The imported ones stay reachable via
the import name, e.g. libA::getValue().
Non-public functions, procedures and global variables are only visible within their own source file, so they may share
names and signatures with symbols of other source files. Public ones, however, are visible to the linker under a name that
only consists of their name and signature (plus the struct name for methods), but not of the source file. Therefore,
two public functions or procedures with the same name and signature, or two public global variables with the same name,
must not exist in the same program. The compiler reports each of these collisions with a
Multiple exported symbols with the same linker name error:
| Spice | |
|---|---|
Linking against source files with another main function
Spice supports linking against source files which include a main function. Although the main function in the imported source file will be ignored and only the main function in the main source file will be taken into account.
Importing functionality from the standard library¶
Every import starting with std/, will be automatically assigned to the standard library.
Importing external packages¶
Spice Shaker installs external packages to the spice_packages directory of your
project. Import their source files like your own ones, relative to the importing source file:
| Spice | |
|---|---|
The compiler does not print warnings or lint findings for the source files of external packages. A source file belongs to an
external package if its import path leads through a spice_packages directory, or if a source file of an external package
imports it with a relative import path. Source files that a package imports with the @/ prefix belong to your project, so
the compiler still prints their warnings.
The directory of a package in spice_packages may be a link to a directory anywhere else, e.g. for path dependencies. An
imported source file has to stay within the directory of its package though, so a link inside a package that leads out of it
is rejected with Import path '...' escapes the permitted directory tree.
Circular imports¶
Source files may import each other, directly or transitively. Spice resolves such circular imports automatically — you simply write the imports you need, no special handling required.
A common case is two structs that reference each other: struct A (in a.spice) holds a pointer to B, and struct B
(in b.spice) holds a pointer back to A.
a.spice:
b.spice:
main.spice:
| Spice | |
|---|---|
Here a.spice and b.spice import each other, forming a cycle in the dependency graph. The compiler handles the cycle
internally, so the B* field in A is type-identical to a B resolved through b.spice, and vice versa. Mutually
recursive functions spread across files work the same way.
Platform dependent imports¶
If you have written code, that only works on one particular platform (e.g. two function with the same functionality, one for Linux
and one for Windows), you can name source files with the suffix of the OS e.g. example_linux.spice / example_windows.spice.
Then you can import the files without the suffix: import "example" as example;. Depending on your target triple, the matching
source file will be chosen at compile time automatically.