This tutorial explains how to add your own module so the Skink compiler can find it.
How Skink finds modules
When you write import "mylib", Skink looks for mylib.skink in these places, in order:
The directory of the file being compiled.
SKINK_HOME, which is the root of the Skink distribution.
SKINK_HOME/std/.
So import "std/time" resolves to std/time.skink under SKINK_HOME.
Adding a personal module
Create mylib.skink next to your main program:
// mylib.skink
module mylib
pub fn Greet(name: string) -> string {
return "Hello, " + name
}
Then use it:
// main.skink
module main
import "mylib"
fn main() -> int {
print(mylib.Greet("Skink"))
return 0
}
Adding a module to std/
If you want the module to be part of the standard library:
Create std/mymodule.skink in the Skink source tree.
Use module mymodule at the top of the file.
Export the public API with pub fn and pub struct.
Rebuild the skink binary if the compiler caches std modules.
Module names and file paths
The module name inside the file does not have to match the file name, but matching makes imports predictable. Use lowercase names to avoid issues with case-sensitive file systems.
Exercises
- Create a stringsutil.skink module with Reverse and IsPalindrome functions.
- Import stringsutil from another file and call both functions.
- Add your module to std/ and run make test to make sure nothing else breaks.