A single file can define multiple namespaces, and a single namespace's definition can be spread across multiple files. However, I have not seen either of those things done very often in Clojure projects. By far the most common approach is to have one file per namespace, and each namespace defined completely within one file. More details below.
It is a reasonably common mistake to have a file define a namespace, but the file name and namespace do not correspond to each other, and this confuses many tools, including require and use in Clojure, without clear error messages. For this reason, Eastwood [3] checks for this issue in Leiningen projects as one of the first things it does, before doing any other lint checks. Can a single file define multiple namespaces? Yes, it can. However, if you 'require' or 'use' a namespace foo.bar, Clojure will look for a file foo/bar.clj (and starting in Clojure 1.7.0, also first foo/bar.cljc) and read and load that file if one exists, anywhere on your class path. After that, it will throw an exception if the namespace foo.bar does not exist, so that file, or something else loaded as a side effect, should create the namespace foo.bar. If loading file foo/bar.clj also creates a namespace baz.gah by having (ns baz.gah ...) in it, and there is no file baz/gah.clj (nor baz/gah.cljc in Clojure 1.7.0), then you cannot do a require or use on namespace baz.gah without getting an error. Since people usually like having the flexibility of doing require or use on any namespace they wish, they usually put the (ns ...) forms defining them into files with the corresponding names. Can multiple files contribute to the same namespace? Yes, they can. Clojure does this itself in the file core.clj [1], and a few other source files, where a 'main' file with a name that corresponds to the namespace name has one or more (load "other_file.clj") calls in it, and those other files have no (ns ...) form at all (for example the file core_proxy.clj [2]). [1] https://github.com/clojure/clojure/blob/master/src/clj/clojure/core.clj#L6488-L6495 [2] https://github.com/clojure/clojure/blob/master/src/clj/clojure/core_proxy.clj [3] https://github.com/jonase/eastwood Andy On Thu, Jun 4, 2015 at 7:55 PM, Todd Stout <[email protected]> wrote: > What are the scope rules of a namespace? For example, can a single file > define multiple namespaces? Can multiple files contribute to the same > namespace? > > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to [email protected] > Note that posts from new members are moderated - please be patient with > your first post. > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en > --- > You received this message because you are subscribed to the Google Groups > "Clojure" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to [email protected] Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
