2.23 Limiting Scope: define-package, open-package, ...
(require scheme/package) |
The bindings documented in this section are provided by the scheme/package library, not scheme/base or scheme.
| |||||||||||||||||||||||
|
The define-package form is based on the module form of Chez Scheme [Waddell99].
The define-package form is similar to module, except that it can appear in any definition context. The forms within a define-package form can be definitions or expressions; definitions are not visible outside the define-package form, but exports determines a subset of the bindings that can be made visible outside the package using the definition form (open-package package-id).
The (id ...) and #:only (id ...) exports forms are equivalent: exactly the listed ids are exported. The #:all-defined form exports all definitions from the package body, and #:all-defined-except (id ...) exports all definitions except the listed ids.
All of the usual definition forms work within a define-package body, and such definitions are visible to all expressions within the body (and, in particular, the definitions can refer to each other). However, define-package handles define*, define*-syntax, define*-values, define*-syntaxes, and open*-syntaxes specially: the bindings introduced by those forms within a define-package body are visible only to forms that appear later in the body, and they can shadow any binding from preceding forms (even if the preceding binding did not use one of the special #<procedure:*> definition forms). If an exported identifier is defined multiple times, the last definition is the exported one.
Examples: | |||
| |||
> doll | |||
reference to undefined identifier: doll | |||
> robot | |||
reference to undefined identifier: robot | |||
> (open-package presents) | |||
> doll | |||
"Molly Coddle" | |||
> robot | |||
reference to undefined identifier: robot | |||
| |||
> (open-package big-russian-doll) | |||
> (open-package middle-russian-doll) | |||
> little-russian-doll | |||
"Anastasia" |
(package-begin form ) |
Similar to define-package, but it only limits the visible of definitions without binding a package name. If the last form is an expression, then the expression is in tail position for the package-begin form, so that its result is the package-begin result.
A package-begin form can be used as an expression, but if it is used in a context where definitions are allowed, then the definitions are essentially spliced into the enclosing context (though the defined bindings remain hidden outside the package-begin).
Examples: | |||
| |||
("mimi") | |||
> secret | |||
reference to undefined identifier: secret |
| |
| |
| |
| |
|
Equivalent to define, define-values, define-syntax, define-syntaxes, and open-package, except within a define-package or package-begin form, where they create bindings that are visible only to later body forms.
Examples: | ||||
| ||||
> (open-package mail) | ||||
> cookies | ||||
(chocolate-chip sugar) | ||||
> (define-syntax-rule (define-seven id) (define id 7)) | ||||
| ||||
| ||||
compile: access from an uncertified context to unexported | ||||
variable from module: | ||||
"/var/tmp/plt/collects/scheme/package.ss" in: | ||||
reverse-mapping |