Is it possible to split a Clojure namespace over multiple source files when doing ahead-of-time compilation with :gen-class
? How do (:main true)
and (defn- ...)
come into play?
相关问题
- Angular: ngc or tsc?
- Better Sequence Duplicate Remover
- Where is the implementation of included C++/C head
- Installation of Leiningen 2.X in Mac OS X
- Using a namespace twice
相关文章
- Factor Clojure code setting many different fields
- Does learning one Lisp help in learning the other?
- php module does not compile. Does not recognize “s
- Why does the C++ compiler give errors after lines
- Compile drools guided decision table into rules
- Better way to nest if-let in clojure
- Idiomatic approach for structuring Clojure source
- How to use default php classes in my namespace?
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 insrc/clj/clojure
: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 isclojure/core.clj
, and it starts with anns
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:Then at appropriate places in your primary file (most commonly all at the end) use
load
to bring in your helper files. Inclojure.core
it looks like this: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: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: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.