0.45+9.2.0.3

8.3 Module Paths🔗ℹ

A module path is a reference to a module, as used with import or as the language module path after ~lang in a module form. It can be any of several forms.

module path

string

A string module path is a relative path using Unix-style conventions: / is the path separator, .. refers to the parent directory, and . refers to the same directory. The string must not start or end with a path separator.

The path is relative to the enclosing file, if any, or it is relative to the current directory.

Module Basics shows examples using relative paths.

module path

collection_module_path

 

collection_module_path

 = 

id

 | 

id / collection_module_path

A module path that is an identifier or /-separated sequence of identifiers refers to an installed library. The identifiers are constrained to contain only ASCII letters, ASCII numbers, +, -, and _. The identifiers in such a collection path refer to collections and subcollections, instead of directories and subdirectories.

An example of this form is rhombus/measure. It refers to the module whose source is the "measure.rhm" file in the "rhombus" collection, which is installed as part of Rhombus. The ".rhm" suffix is added automatically.

#lang rhombus

 

import:

  rhombus/measure

 

measure.cpu_milliseconds()

module path

lib(rel_string)

A lib path is like an identifier path, but expressed as a string instead of /-separated identifiers. A rel_string can end with a file suffix, in which case ".rhm" is not automatically added. If rel_string does not contain /, then "/main.rhm" is appended to the string when forming the module file path.

To refer to a Racket module, use a lib path with a ".rkt" suffix.

#lang rhombus

 

import:

  lib("racket/math.rkt") as rkt_math

 

rkt_math.pi  // prints 3.141592653589793

module path

file(string)

A file path refers to a file, where string is a relative or absolute path using the current platform’s conventions. This form is not portable, and it should not be used when a plain, portable string suffices.

module path

module_path ! id

A path with ! refers to the submodule named id of the module referenced by module_path. Multiple uses of ! can be chained to reach more deeply nested submodules.

module zoo ~lang rhombus:

  module monkey_house ~lang rhombus:

    export:

      monkey

    def monkey = "Curious George"

 

import self!zoo!monkey_house open

monkey

module path

self

 

module path

parent

The self module path form can be used only with ! to refer to a submodule. The form self!id refers to a submodule named id of the enclosing module. Additional uses of ! refer to more deeply nested submodules within that one. In an interactive context such as a REPL, self!id refers to a module declared interactively with name id.

The form parent refers to the parent of an enclosing submodule. A parent form may be followed by !id to access a sibling submodule. Additional ! operators immediately after parent! reach further up the enclosing-module chain.

module zoo ~lang rhombus:

  module monkey_house ~lang rhombus:

    export:

      monkey

    def monkey = "Curious George"

  module crocodile_house ~lang rhombus:

    import:

      parent!monkey_house open

    export:

      dinner

    def dinner = monkey

 

import self!zoo!crocodile_house open

dinner

module path

. id

 

module path

module_path . id_or_op

A prefix . refers to an import prefix or a namespace id in the enclosing environment, not to a separate module. An infix . refers to an exported id (or operator op) provided by a module_path. When the last id in a dotted sequence is not itself a namespace, the dotted form is a shorthand for exposing just that binding from module_path.

import:

  rhombus/measure.cpu_milliseconds

 

cpu_milliseconds()