Namespaces and compound symbols

A compound symbol is one whose name is unique within a specific namespace, rather than globally. It is written using the infix colon operator:

namespace:local-name

A namespace is a mapping from local-name strings to symbols. A simple namespace is named by a string, and there is only one such namespace by that name. A namespace is usually referenced using a shorter namespace prefix, as in this example:

(define-namespace geom "http://foo.org/lib/geometry")
(define (geom:translate x y)
  (java.awt.geom.AffineTransform:getTranslateInstance x y))
(define geom:zero (geom:translate 0 0))
geom:zero
  ⇒ AffineTransform[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]

Function: namespace namespace-name

Return the simple namespace whose name is the namespace-name, a string. If no such namespace exists, create it. The namespace-name is commonly a URI, especially when working with XML, in which case it is called a namespace-URI. However, any non-empty string is allowed. Two namespaces are the same (eq?) if they have the same (equal?) namespace-names.

You can use a namespace-value directly in a compound name:

(namespace "http://foo.org/lib/geometry"):zero
  ⇒ AffineTransform[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]

The reader macro #,namespace is equivalent to the namespace function, but it is invoked at read-time:

(eq? #,(namespace "foo") (namespace "foo")) ⇒ #t

Syntax: define-namespace name namespace-name

Defines name as a namespace prefix - a lexically scoped "nickname" for the namespace (or package) whose full name is namespace-name, which should be a string literal.

Any symbols in the scope of this definitions that contain a colon, and where the part before the colon matches the name will be treated as being in the package/namespace whose global unique name is the namespace-name.

Has mostly the same effect as:

(define-constant name #,(namespace namespace-name)

However, using define-namespace (rather than define-constant) is recommended if you want to use compound symbols as names of variables, especially local variables, or if you want to quote compound symbols.

(A namespace is similar to a Common Lisp package, and the namespace-name is like the name of the package. However, a namespace alias belongs to the lexical scope, while a Common Lisp package nickname is global and belongs to the package itself.)

If the namespace-name starts with the string "class:", then the name can be used for invoking Java methods (see Calling Java methods from Scheme) and accessing fields (see Accessing fields of Java objects).

Syntax: define-private-namespace name namespace-name

Same as define-namespace, but the prefix name is local to the current module.

The variation define-xml-namespace is used for Creating XML nodes.