Splitting a Clojure namespace over multiple files?

Certainly you can, in fact clojure. Core namespace itself is split up this way and provides a good model which you can follow by looking in src/clj/clojure.

Up vote 25 down vote favorite 8 share g+ share fb share tw.

Namespaces clojure compilation link|improve this question asked Jan 14 '11 at 11:54Ralph5,17421037 99% accept rate.

Overview Certainly you can, in fact clojure. Core namespace itself is split up this way and provides a good model which you can follow by looking in src/clj/clojure: core. Clj core_deftype.

Clj core_print. Clj core_proxy. Clj ..etc.. All these files participate to build up the single clojure.

Core namespace. Primary File One of these is the primary file, named to match the namespace name so that it will be found when someone mentions it in a :use or :require. In this case the main file is clojure/core.

Clj, and it starts with an ns form. This is where you should put all your namespace configuration, regardless of which of your other files may need them. This normally includes :gen-class as well, so something like: (ns my.lib.of.

Excellence (:use clojure.java.io :as io :only reader) (:gen-class :main true)) Then at appropriate places in your primary file (most commonly all at the end) use load to bring in your helper files. In clojure. Core it looks like this: (load "core_proxy") (load "core_print") (load "genclass") (load "core_deftype") (load "core/protocols") (load "gvec") Note that you don't need the current directory as a prefix, nor do you need the .

Clj suffix. Helper files Each of the helper files should start by declaring which namespace they're helping, but should do so using the in-ns function. So for the example namespace above, the helper files would all start with: (in-ns 'my.lib.of.

Excellence) That's all it takes. Gen-class Because all these files are building a single namespace, each function you define can be in any of the primary or helper files. This of course means you can define your gen-class functions in any file you'd like: (defn -main & args ...) Note that Clojure's normal order-of-definition rules still apply for all functions, so you need to make sure that whatever file defines a function is loaded before you try to use that function.

Private Vars You also asked about the (defn- foo ...) form which defines a namespace-private function. Functions defined like this as well as other :private vars are visible from within the namespace where they're defined, so the primary and all helper files will have access to private vars defined in any of the files loaded so far.

1 Very nice, complete answer! BTW, I'm almost finished on my first pass through The Joy of Clojure. Great book!

– Ralph Jan 14 '11 at 14:38.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions