29

I have read in several places that single segment namespaces are discouraged in clojure.

Indeed almost every library I've seen has (require 'lib.core) instead of (require 'lib).

Why?


Edit: I am a bit stupid. Extra credit will be given for a concise example of how a single segment namespace might be a bad thing.

1
  • 1
    Ya, but if you're just messing around with Clojure, trying things out, and don't care, I use single segment namespaces all the time, so much more convenient not to have to create a directory layout. For real projects, don't do it, because of what others have said.
    – Didier A.
    Commented Apr 18, 2018 at 21:14

2 Answers 2

28

Java discourages the use of the default package because you can't refer to anything in it from any other package. If you pre-compile a one-segment Clojure namespace, you'll get a Java class in the default package. If anyone at any time wants to use your library from Java, he will be stopped dead by this triviality. As there is no good reason in favor of using a single-segment namespace, and there is this one well-defined reason not to, it is fair to say that single-segment namespaces should be a discouraged practice in Clojure.

3
  • 4
    ok, so doom.clj -> doom.class but (ns horror.doom) in horror/doom.clj goes to doom.class in package horror? And I have to have the subdirectory horror in order to use (ns horror.doom)? Commented Nov 26, 2012 at 16:39
  • 4
    Yes, that's the convention by which Clojure automatically locates .clj files based on namespace. Commented Nov 26, 2012 at 17:11
  • >As there is no good reason in favor of using a single-segment namespace Simplicity seems like a good enough reason to me, if we're talking about what "should be" as opposed to what is currently technically possible. Commented Jul 30, 2020 at 21:42
10

Single segment namespaces have issues with Java interop. Some things may break in unexpected ways. See also https://groups.google.com/forum/?fromgroups=#!topic/clojure/gOffhotk25Y

2
  • 1
    I read that and I'm still not sure what's going on. The best I can translate it is 'There's some repulsive java-abortion poking through into clojure but it's not clear how it could actually be bad.' Thanks anyway. Commented Nov 26, 2012 at 15:27
  • 5
    In clojure on the JVM, (ns foo.bar) results in class bar in package foo, with the functions defined in bar as inner classes of bar. This means "toplevel" namespaces in clojure are actually "package-less" classes, which are handled specially (in some possibly unexpected ways) by the JVM and/or Java. Commented Nov 26, 2012 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.