[gambit-list] Valid namespace prefix
Marc Feeley
feeley at iro.umontreal.ca
Mon Jul 30 07:58:02 EDT 2007
On 30-Jul-07, at 1:42 AM, James Long wrote:
> How come Gambit enforces the module# syntax for namespaces? I have
> just started using them, and have looked at many posts describing the
> system, but I don't remember anyone explaining the reason for this
> syntax.
>
> For example, trying anything different than module gives an error:
>
>> (namespace ("foo:"))
> *** ERROR IN (console)@1.13 -- Ill-formed namespace prefix
>
> I would like to use the colon syntax as I find it much more readable,
> especially for user-land code. I'm going to usually use fully
> qualified names, so this slight readability improvement is important.
> Is there a reason I shouldn't be able to do this?
Yes. It is so that the Scheme front-end (in the compiler and
interpreter) can detect that an identifier is a "fully qualified"
identifier and handle them accordingly. Fully qualified
identifiers must have an embedded "#", as in foo#bar or foo#bar#baz
or ##car. Fully qualified identifiers bypass the effects of the
namespace declaration (i.e. the namespace prefix is not automatically
added).
For example, in the following program containing no namespace
declaration:
(define (baz) (list hot#stuff my:stuff))
the front-end will not remap the identifiers, giving:
define -> define
baz -> baz
list -> list
hot#stuff -> hot#stuff
my:stuff -> my:stuff
If we add the namespace declaration (namespace ("foo#")), i.e.
(namespace ("foo#"))
(define (baz) (list hot#stuff my:stuff))
the front-end will remap all the "non fully qualified" identifiers,
giving:
define -> foo#define
baz -> foo#baz
list -> foo#list
hot#stuff -> hot#stuff
my:stuff -> foo#my:stuff
Note that because of this namespace declaration the expression
(define ...) is no longer a procedure definition. To fix this we
need to prevent the identifier "define" (and "list") to be remapped.
We can do this several ways. Either:
(namespace ("foo#") ("" define list))
(define (baz) (list hot#stuff my:stuff))
or
(namespace ("foo#"))
(##namespace ("" define list))
(define (baz) (list hot#stuff my:stuff))
or
(namespace ("foo#") ("" namespace))
(namespace ("" define list))
(define (baz) (list hot#stuff my:stuff))
or
(namespace ("foo#"))
(##include "~~/lib/gambit#.scm")
(define (baz) (list hot#stuff my:stuff))
Marc
More information about the Gambit-list
mailing list