14.1 Namespaces
See Namespaces for basic information on the namespace model.
A new namespace is created with procedures like make-empty-namespace, and make-base-namespace, which return a first-class namespace value. A namespace is used by setting the current-namespace parameter value, or by providing the namespace to procedures such as eval and eval-syntax.
procedure
(namespace? v) → boolean?
v : any/c
procedure
The new namespace is associated with a new root namespace, which has the same module registry as the returned namespace and has a base phase of 0. The new root namespace is the same as the returned namespace if both have base phase 0.
procedure
procedure
syntax
This form can be used only in a top-level context or in a module-context.
procedure
(namespace-anchor? v) → boolean?
v : any/c
procedure
a : namespace-anchor?
If the anchor is from a define-namespace-anchor form in a module context, then the source is the namespace in which the containing module is instantiated. If the anchor is from a define-namespace-anchor form in a top-level content, then the source is the namespace in which the anchor definition was evaluated.
procedure
a : namespace-anchor?
If the anchor is from a define-namespace-anchor form in a module context, then the result is a namespace for the module’s body in the anchor’s phase. The result is the same as a namespace obtained via module->namespace, and the module is similarly made available if it is not available already.
If the anchor is from a define-namespace-anchor form in a top-level content, then the result is the namespace in which the anchor definition was evaluated.
parameter
(current-namespace n) → void? n : namespace?
procedure
sym : symbol?
procedure
(namespace-base-phase [namespace]) → exact-integer?
namespace : namespace? = (current-namespace)
procedure
(namespace-module-identifier [where]) → identifier?
where : (or/c namespace? exact-integer? #f) = (current-namespace)
The lexical information of the identifier includes bindings (in the same phase level) for all syntactic forms that appear in fully expanded code (see Fully Expanded Programs), but using the name reported by the second element of identifier-binding for the binding; the lexical information may also include other bindings.
procedure
(namespace-variable-value sym [ use-mapping? failure-thunk namespace]) → any sym : symbol? use-mapping? : any/c = #t failure-thunk : (or/c (-> any) #f) = #f namespace : namespace? = (current-namespace)
If use-mapping? is true (the default), and if sym maps to a top-level variable or an imported variable (see Namespaces), then the result is the same as evaluating sym as an expression. If sym maps to syntax or imported syntax, then failure-thunk is called or the exn:fail:syntax exception is raised. If sym is mapped to an undefined variable or an uninitialized module variable, then failure-thunk is called or the exn:fail:contract:variable exception is raised.
If use-mapping? is #f, the namespace’s syntax and import mappings are ignored. Instead, the value of the top-level variable named sym in namespace is returned. If the variable is undefined, then failure-thunk is called or the exn:fail:contract:variable exception is raised.
If failure-thunk is not #f, namespace-variable-value calls failure-thunk to produce the return value in place of raising an exn:fail:contract:variable or exn:fail:syntax exception.
procedure
(namespace-set-variable-value! sym v [ map? namespace as-constant?]) → void? sym : symbol? v : any/c map? : any/c = #f namespace : namespace? = (current-namespace) as-constant? : any/c = #f
If map? is supplied as true, then the namespace’s identifier mapping is also adjusted (see Namespaces) in the phase level corresponding to the base phase, so that sym maps to the variable.
If as-constant? is true, then the variable is made a constant (so future assignments are rejected) after v is installed as the value.
Changed in version 6.90.0.14 of package base: Added the as-constant? argument.
procedure
(namespace-undefine-variable! sym [ namespace]) → void? sym : symbol? namespace : namespace? = (current-namespace)
procedure
(namespace-mapped-symbols [namespace]) → (listof symbol?)
namespace : namespace? = (current-namespace)
procedure
(namespace-require quoted-raw-require-spec [ namespace]) → void? quoted-raw-require-spec : any/c namespace : namespace? = (current-namespace)
Module paths in quoted-raw-require-spec are resolved with respect to current-load-relative-directory or current-directory (if the former is #f), even if the current namespace corresponds to a module body.
Changed in version 6.90.0.16 of package base: Added the namespace optional argument.
procedure
(namespace-require/copy quoted-raw-require-spec [ namespace]) → void? quoted-raw-require-spec : any/c namespace : namespace? = (current-namespace)
Changed in version 6.90.0.16 of package base: Added the namespace optional argument.
procedure
(namespace-require/constant quoted-raw-require-spec [ namespace]) → void? quoted-raw-require-spec : any/c namespace : namespace? = (current-namespace)
Changed in version 6.90.0.16 of package base: Added the namespace optional argument.
procedure
(namespace-require/expansion-time quoted-raw-require-spec [ namespace]) → void? quoted-raw-require-spec : any/c namespace : namespace? = (current-namespace)
Changed in version 6.90.0.16 of package base: Added the namespace optional argument.
procedure
(namespace-attach-module src-namespace modname [ dest-namespace]) → void? src-namespace : namespace? modname : (or module-path? resolved-module-path?) dest-namespace : namespace? = (current-namespace)
In addition to modname, every module that it imports (directly or indirectly) is also recorded in the current namespace’s module registry, and instances at the same phase are also attached to dest-namespace (while visits at the module’s phase and instances at higher or lower phases are not attached, nor even made available for on-demand visits). The inspector of the module invocation in dest-namespace is the same as inspector of the invocation in src-namespace.
If modname is not a symbol, the current module name resolver is called to resolve the path, but no module is loaded; the resolved form of modname is used as the module name in dest-namespace.
If modname refers to a submodule or a module with submodules, unless the module was loaded from bytecode (i.e., a ".zo" file) independently from submodules within the same top-level module, then declarations for all submodules within the module’s top-level module are also attached to dest-namespace.
If modname does not refer to an instantiated module in src-namespace, or if the name of any module to be attached already has a different declaration or same-phase instance in dest-namespace, then the exn:fail:contract exception is raised.
If src-namespace and dest-namespace do not have the same base phase, then the exn:fail:contract exception is raised.
Unlike namespace-require, namespace-attach-module does not instantiate the module, but copies the module instance from the source namespace to the target namespace.
> (module food racket/base (provide apple) (define apple (list "pie"))) > (namespace-require ''food) > (define ns (current-namespace))
> (parameterize ([current-namespace (make-base-namespace)]) (namespace-require ''food)) require: unknown module
module name: 'food
> (parameterize ([current-namespace (make-base-namespace)]) (namespace-attach-module ns ''food) (namespace-require ''food) (eq? (eval 'apple) apple)) #t
> (parameterize ([current-namespace (make-base-namespace)]) (namespace-attach-module-declaration ns ''food) (namespace-require ''food) (eq? (eval 'apple) apple)) #f
procedure
(namespace-attach-module-declaration src-namespace modname [ dest-namespace]) → void? src-namespace : namespace? modname : module-path? dest-namespace : namespace? = (current-namespace)
procedure
(namespace-unprotect-module inspector modname [ namespace]) → void? inspector : inspector? modname : module-path? namespace : namespace? = (current-namespace)
procedure
(namespace-module-registry namespace) → any
namespace : namespace?
procedure
(namespace-call-with-registry-lock namespace thunk) → any namespace : namespace? thunk : (-> any)
Namespace functions do not automatically use the registry lock, but it can be used via namespace-call-with-registry-lock among threads that load and instantiate modules to avoid internal race conditions. On-demand instantiation of available modules also takes the lock; see Module Expansion, Phases, and Visits.
Added in version 8.1.0.5 of package base.
procedure
(module->namespace mod [src-namespace]) → namespace?
mod :
(or/c module-path? resolved-module-path? module-path-index?) src-namespace : namespace? = (current-namespace)
Module paths in a top-level require expression are resolved with respect to the namespace’s module. New provide declarations are not allowed.
If the current code inspector does not control the invocation of the module in src-namespace’s module registry, the exn:fail:contract exception is raised; see also Code Inspectors.
Bindings in the result namespace cannot be modified if the compile-enforce-module-constants parameter was true when the module was declared, unless the module declaration itself included assignments to the binding via set!.
Changed in version 6.90.0.16 of package base: Added the src-namespace optional argument.
procedure
(namespace-syntax-introduce stx [namespace]) → syntax?
stx : syntax? namespace : namespace? = (current-namespace)
Changed in version 6.90.0.16 of package base: Added the namespace optional argument.
procedure
(module-provide-protected? module-path-index sym) → boolean? module-path-index : (or/c symbol? module-path-index?) sym : symbol?
The module-path-index argument can be a symbol; see Compiled Modules and References for more information on module path indices.
Typically, the arguments to module-provide-protected? correspond to the first two elements of a list produced by identifier-binding.
procedure
(variable-reference? v) → boolean?
v : any/c
procedure
(variable-reference-constant? varref) → boolean?
varref : variable-reference?
procedure
(variable-reference->empty-namespace varref) → namespace?
varref : variable-reference?
procedure
(variable-reference->namespace varref) → namespace?
varref : variable-reference?
If varref refers to a top-level variable, then the result is the namespace in which the referenced variable is defined.
procedure
→ (or/c resolved-module-path? #f) varref : variable-reference?
If varref refers to a top-level variable, then the result is #f.
procedure
→ (or/c module-path-index? #f) varref : variable-reference?
If varref refers to a top-level variable, then the result is #f.
procedure
(variable-reference->module-source varref)
→ (or/c symbol? (and/c path? complete-path?) #f) varref : variable-reference?
If varref refers to a top-level variable, then the result is #f.
procedure
(variable-reference->phase varref) → exact-nonnegative-integer?
varref : variable-reference?
procedure
(variable-reference->module-base-phase varref) → exact-integer?
varref : variable-reference?
For a variable with a module, the result is less than the result of (variable-reference->phase varref) by n when the variable is bound at phase level n within the module.
procedure
→ inspector? varref : variable-reference?
procedure
(variable-reference-from-unsafe? varref) → boolean?
varref : variable-reference?
The variable-reference-from-unsafe? procedure is intended for use as
which the compiler can optimize to a literal #t or #f (since the enclosing module is being compiled in unsafe mode or not).
Added in version 6.12.0.4 of package base.