Janet 1.0.0-dev Documentation

Modules

As programs grow, they should be broken into smaller pieces for maintainability. Janet has the concept of modules to this end. A module is a collection of bindings and it's associated environment. By default, modules correspond one to one with source files in Janet, although you may override this and structure modules however you like.

Importing a Module

To use a module, the best way is use the (import) macro, which looks for a module with the given name on your system and imports it's symbols into the current environment, usually prefixed with the name of the module.

A default install of Janet, installed via make install, comes with a few modules out of the box. One such module is cook, which is a tool to help build artifacts for Janet, including native modules.

(import cook)

(cook/shell "ls -l")

Once cook is imported, all of it's symbols are available to the host program, but prefixed with cook/. To import the symbols with a custom prefix or without any prefix, use the :prefix argument to the import macro.

(import cook :prefix "")

(shell "ls -l")

You may also use the :as argument to specify the prefix in a more natural way.

(import cook :as ck)

(ck/shell "ls -l")

Writing a module

Writing a module in Janet is mostly about exposing only the public functions that you want users of your module to be able to use. All top level functions defined via defn, macros defined defmacro, constants defined via def, and and vars defined via var will be exported in your module. To mark a function or binding as private to your module, you may use defn- or def- at the top level. You can also add the :private metadata to the binding.

module/paths

To understand how module loading in Janet works, it's necessary to understand module/paths. This is an array of file paths to search for modules in the file system. Each element in this array is a pair [path type], where path is the a templated file path, which determines what path corresponds to a module name, and where type is the loading method used to load the module. Loading can be one of :native, :source, or :image.