On Thu, Jun 23, 2011 at 18:48, Ken Wesson <kwess...@gmail.com> wrote:
> On Thu, Jun 23, 2011 at 11:55 AM, B Smith-Mannschott
> <bsmith.o...@gmail.com> wrote:
>> user> (name 'a/b/c)
>> "c"
>> user> (namespace 'a/b/c)
>> "a/b"
>>
>> Is this intentional? I would have expected a/b/c to be
>> rejected as a symbol name since we use slashes to separate
>> namespace from name and conventionally use "." to indicate
>> hierarchy in namespace names.
>
> => (namespace (symbol "" "a/b"))
> ""
> => (name (symbol "" "a/b"))
> "a/b"
>
> Both the namespace and the name can contain slashes, but the normal
> reader behavior is that foo/bar/baz is interpreted as namespace up to
> the last slash and then name. Unless the symbol is just "/", in which
> case the reader treats that as the name, with no namespace. (Necessary
> for a bare / to resolve, ordinarily, to the division function
> clojure.core//). You can get any desired symbol using the two-argument
> form of the symbol function, though.
>
> There's some ickiness here. For instance the one-argument form of
> symbol doesn't parse the same as the reader does:
>
> => (clojure.core// 42 2)
> 21
> => (eval (list (symbol "clojure.core//") 42 2))
> #<CompilerException java.lang.StringIndexOutOfBoundsException: String
> index out of range: 0 (NO_SOURCE_FILE:1)>
> => (eval (list (symbol "clojure.core" "/") 42 2))
> 21
>
> In fact, (symbol "clojure.core//") produces a symbol with namespace
> "clojure.core/" and name "", the latter of which apparently leads to
> the rather obfuscatory error message above.
>
> Symbols whose names contain slashes don't work as vars -- another
> cryptic message, since the symbol's actually unqualified here:
>
> => (eval (list 'def (symbol "" "foo/bar") 42))
> #<CompilerException java.lang.Exception: Can't refer to qualified var
> that doesn't exist (NO_SOURCE_FILE:1)>
>
> And symbols whose namespaces contain slashes won't work as vars with
> AOT, if at all, since the namespace name ends up a class file name and
> slashes are path separator characters on every sane filesystem and
> thus cannot appear in file names.
>
> Moral of the story: try to avoid slashes in symbol names, except for
> the unqualified name of the division operator. :) But if you really
> for some reason need one it can be done, though the only apparent use
> for such a symbol is as a kind of auto-interning string rather than as
> a var name. You'd probably be better off using a keyword for such
> uses.

Thanks!

My question probably should have been: is it intentional that the Clojure reader
accepts symbol names containing more than one slash, producing a
namespace portion
of the symbol containing slashes in its name?

I've been reading through LispReader.java and porting bits of it to
Clojure TDD-style
as a finger exercise. That's how such questions arise.

// ben

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to