On this page:
import
open
typed
module

9 Modules and Imports🔗ℹ

A Shplait program that starts with #lang shplait is a module, and its definitions are all implicitly exported for use by other modules. Use import in a module to import definitions from other modules. The module form adds to a submodule, which is nested inside another module.

definition

import:

  import_spec

  ...

 

import_spec

 = 

module_path

 | 

open:

  module_path

  ...

 | 

typed module_path:

  in_name :: type

  ...

 

module_path

 = 

id

 | 

id / module_path

 | 

lib(module_string)

 | 

relative_path_string

 | 

file(path_string)

 

in_name

 = 

id

 | 

id ~as id

 | 

id . in_name

 

import

open: module_path; ...

 

import

typed module_path: in_name :: type; ...

The import form acts like a definition for all of the exports of another module, making them available in the current definition context. Typically, import is used in an immediate module body, but it also can be used in a nested form, such as a block form.

If open is not used, then the last component of module_path (not counting a file suffix, if any), is used to prefix all of the imported names. Use the prefix, then ., then a name exported from the module to use that name. If open is used for the module, then its exported names can be used directly, without a prefix.

The typed form is for importing functions from modules that are written in a language other than Shplait. The specific bindings named by the in_names are imported from the module_path, and each imported binding is assumed (without specific compile-time or run-time checking) to have the specified type. An in_name can use . to access nested exports from module_path, in which case the last id is used as the name of the import. If in_name includes ~as, then the id after ~as is the local name. See type for an example.

The relative_path_string form allows only characters in a file name that are especially portable: a-z, A-Z, 0-9, -, +, _, /, and .. Use the file(path_string) form when a more general, platform-specific path_string is needed.

Some operating systems, such as Windows or macOS, may hide file extensions when listing files. A relative_path_string or path_string must include a file’s extension, if any, which is typically ".rhm".

declaration

module id:

  defn_or_expr

  ...

 

defn_or_expr

 = 

defn

 | 

expr

Adds to the submodule name id within the enclosing module. Typically, id is test, which adds to a submodule that DrRacket automatically runs after the enclosing module, so that tests within the submodule can run after all definitions are ready. The submodule name main is also special; it is run when the enclosing module is run as a main program, but not when the enclosing module is used as imported into another module.