Next: Global Names Up: Threads and Namespaces Previous: Semaphores

# Global Variable Namespaces

MzScheme supports multiple global variable namespaces. A new namespace is created with the `make-namespace` procedure, which returns a first-class namespace value. A namespace is used by setting the `current-namespace` parameter value (see section 9.4.1.6).

The current namespace is used by `eval`, `load`, `compile`, and `expand-defmacro`. Once an expression is `eval`ed or `compile`d, the global variable references in the compiled expression are permanently attached to a particular namespace, so the current namespace at the time that the code is executed is not used as the namespace for referencing global variables in the expression.

Example: ``

      (define x 'orig) ; define in the original namespace
      ; The following let expression is compiled in the original
      ; namespace, so direct references to x see 'orig.
      (let ([n (make-namespace)]) ; make new namespace
        (parameterize ([current-namespace n])
          (eval '(define x 'new)) ; evals in the new namespace
          (display x) ; displays 'orig
          (display (eval 'x)))) ; displays 'new

`(make-namespace flag-symbol )` creates a new namespace; the flag-symbols are options that determine the kinds of global names that are initially bound in the new namespace. Any number of flag-symbolss can be specified, where each flag-symbol is one of the following symbols:

- `'keywords` -- keywords are enforced in the new namespace
- `'no-keywords` -- keywords are not enforced in the new namespace
- `'call/cc=call/ec` -- `call/cc` captures an escape-only continuation in the new namespace
- `'call/cc!=call/ec` -- `call/cc` captures a full continuation in the new namespace
- `'hash-percent-syntax` -- only syntactic forms prefixed with `#%` are present in the new namespace
- `'all-syntax` -- all MzScheme syntactic forms are present in the new namespace
- `'hash-percent-globals` -- only globals prefixed with `#%` are present in the new namespace
- `'all-globals` -- all MzScheme globals are present in the new namespace
- `'empty` -- no names are initially bound in the new namespace

Applications embedding MzScheme may extend this list of flags. (MrEd adds the `'mred` flag for installing the built-in MrEd classes and procedures.) If `'empty` is provided, all other provided flags are ignored. Otherwise, if two conflicting flags are provided, the latter flag takes precedence. If any other value or symbol is provided as a flag-symbol, the `exn:application:type` exception is raised. The default settings are built into the executable running MzScheme.

`(namespace? v)` returns `#t` if v is a namespace value, `#f` otherwise.

The MzScheme versions of the R5RS procedures `scheme-report-environment` and `null-environment` produce namespaces. MzScheme's `scheme-report-environment` supports versions 4 and 5, returning a namespace that includes all `#%` syntactic forms and globals, but otherwise only those procedures and syntax found in the corresponding report. In addition, for version 5, the `"synrule.ss"` library is loaded into the environment if possible (library failures are ignored); see also section 15.2.25.

- Global Names
- Keywords

Next: Global Names Up: Threads and Namespaces Previous: Semaphores