8.4 Imports: import
The import form imports from another module. An import form can appear within a module, in which case it introduces bindings from the specified module into the importing module. An import form can also appear at the top level (in a REPL), in which case it both imports bindings and instantiates the specified module; that is, it evaluates the body definitions and expressions of the specified module, if they have not been evaluated already.
A single import form can specify multiple imports at once using a block of import_clauses:
import_clause
....
Specifying multiple import_clauses in a single import is essentially the same as using multiple import forms, each with a single import_clause.
The simplest import_clause is just a module path. By default, an import clause that names a module binds a prefix derived from the last element of the module path; imports are then accessed using that prefix, ., and the imported name.
color
size
The import_clause shape can be adjusted by attaching modifiers to it.
"f2c.rhm" as convert
Using as ~none suppresses the prefix entirely, so that only exposed names are bound.
import | |
|
"f2c.rhm" open
fahrenheit_to_celsius(fahrenheit_freezing)
Opening an import is sometimes called “namespace dumping” and is discouraged in some cases. When module_path provides macros or a sublanguage, however, it may be intended for use with open. The documentation for each Rhombus module either shows open to suggest that the module is imported that way, or it is documented without open.
"f2c.rhm" expose:
fahrenheit_to_celsius
fahrenheit_to_celsius(f2c.fahrenheit_freezing)
Combining as ~none and expose is similar to Racket’s only-in, where only the listed names are bound.
tastes_great
less_filling
less_filling as lite
tastes_great
m.tastes_great
tastes_great
m.less_filling
The as, open, expose, rename, only, and except modifiers can be combined within a single import clause to implement more complex manipulations of imported bindings. For example,
tastes_great
imports all bindings that m exports, except for tastes_great, and with local names accessed through the prefix my_m.