What's a single-segment namespace and why should it be avoided?

596 views
Skip to first unread message

Evan R. Murphy

unread,
Aug 11, 2010, 10:18:42 PM8/11/10
to Clojure
I've seen recommendations around not to use single-segment namespaces
[1]. What exactly is a single-segment namespace and why should I avoid
using them in my projects?

My guess is that a single-segment namespace is one that's completely
flat instead of hierarchical, as if I were to put my source in src/
foo.clj instead of src/foo/core.clj and declare the namespaces
accordingly. If so, maybe there is some reason relating to interop or
code sharing why this is avoided? I really like the simplicity of src/
foo.clj so please help me understand why it's a bad idea (if it is at
all!).

This is my first post to the group. I hope this question is welcome
and not too terribly redundant or annoying. (I did try searching for
the answer but couldn't find an explanation.)

Thanks,
Evan

[1] For example, from http://github.com/technomancy/leiningen/blob/master/TUTORIAL.md:
"The src/myproject/core.clj file corresponds to the myproject.core
namespace. Note that we use that instead of just myproject since
single-segment namespaces are discouraged in Clojure."

Meikel Brandmeyer

unread,
Aug 12, 2010, 8:09:29 AM8/12/10
to Clojure
Hi,

it's for technical reasons. Clojure wll then generate classes without
a package, which causes them to be put in the default package. There
are problems/restrictions/issues with classes in the default package.
I'm not a JVM expert, so someone else might provide more details. I
just go with the "JVM doesn't like it. Don't do it."

Sincerely
Meikel

Armando Blancas

unread,
Aug 12, 2010, 3:42:01 PM8/12/10
to Clojure
The Java compiler doesn't like it; it won't import it from other
packages. The JVM won't mind. When Clojure code is the consumer, or
with use/require Clojure libs, one-segment namespaces work fine. I
keep a lot of those for test/hacks/trying things out, aot compiled and
not.

Adam Schmideg

unread,
Aug 12, 2010, 3:48:38 PM8/12/10
to Clojure
You can't call anything in the default package from a regular
package. But I think (ns foo) would compile into a foo package, so
this wouldn't be a problem.

Adam

Evan R. Murphy

unread,
Aug 12, 2010, 5:51:39 PM8/12/10
to Clojure
Thanks for responding. I'm glad to know this is about technical
limitations rather than some preference for deeper namespace
hierarchies.

Meikel Brandmeyer

unread,
Aug 13, 2010, 1:28:38 PM8/13/10
to clo...@googlegroups.com
Hi,

Am 12.08.2010 um 21:48 schrieb Adam Schmideg:

> You can't call anything in the default package from a regular
> package. But I think (ns foo) would compile into a foo package, so
> this wouldn't be a problem.

No. It compiles to something like foo__init.class, which lives in the default package. The function contained in the namespace will be in the package foo, though.

Sincerely
Meikel

Reply all
Reply to author
Forward
0 new messages