top
up

6.4Imports: require πŸ”— i

The require form imports from another module. A require form can appear within a module, in which case it introduces bindings from the specified module into the importing module. A require form can also appear at the top level, 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 require can specify multiple imports at once:

(require require-spec...)

Specifying multiple require-specs in a single require is essentially the same as using multiple require s, each with a single require-spec. The difference is minor, and confined to the top-level: a single require can import a given identifier at most once, whereas a separate require can replace the bindings of a previous require (both only at the top level, outside of a module).

The allowed shape of a require-spec is defined recursively:

module-path

In its simplest form, a require-spec is a module-path (as defined in the previous section, Module Paths). In this case, the bindings introduced by require are determined by provide declarations within each module referenced by each module-path.

Examples:
> (module mracket
(provide color)
(define color"blue"))
> (module nracket
(provide size)
(define size17))
> (require 'm'n)
> (list colorsize)

'("blue" 17)

(only-in require-specid-maybe-renamed...)
id-maybe-renamed = id
| [orig-idbind-id]

An only-in form limits the set of bindings that would be introduced by a base require-spec. Also, only-in optionally renames each binding that is preserved: in a [orig-idbind-id] form, the orig-id refers to a binding implied by require-spec, and bind-id is the name that will be bound in the importing context instead of orig-id.

Examples:
> (module m(lib "racket")
(provide tastes-great?
less-filling?)
(define tastes-great?#t)
(define less-filling?#t))
> (require (only-in 'mtastes-great?))
> tastes-great?

#t

> less-filling?

less-filling?: undefined;

cannot reference an identifier before its definition

in module: top-level

> (require (only-in 'm[less-filling?lite?]))
> lite?

#t

(except-in require-specid...)

This form is the complement of only-in : it excludes specific bindings from the set specified by require-spec.

(rename-in require-spec[orig-idbind-id]...)

This form supports renaming like only-in , but leaving alone identifiers from require-spec that are not mentioned as an orig-id.

(prefix-in prefix-idrequire-spec)

This is a shorthand for renaming, where prefix-id is added to the front of each identifier specified by require-spec.

The only-in , except-in , rename-in , and prefix-in forms can be nested to implement more complex manipulations of imported bindings. For example,

(require (prefix-in m:(except-in 'mghost)))

imports all bindings that m exports, except for the ghost binding, and with local names that are prefixed with m:.

Equivalently, the prefix-in could be applied before except-in , as long as the omission with except-in is specified using the m: prefix:

(require (except-in (prefix-in m:'m)m:ghost))

top
up

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /