[previous] [up] [next]     [index]
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.[footnote] Once an expression is evaled or compiled, 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 tex2html_wrap_inline100181 ) 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:

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.




[previous] [up] [next]     [index]
Next: Global Names Up: Threads and Namespaces Previous: Semaphores

PLT