Clojure defs series but don't expose all of them

At the head of my clojure file, I have a series of defs, some of which are just defs stepping, i.e. not intended for future use in the file. I.e.

def src-folder (File. "src") def lib-folder (File. src-folder "lib") def dist-folder (File. src-folder "bin") ;; I only care for the lib-folder and dist-folder beyond this point 

What is the right way to do this in Clojure?

+4
source share
2 answers

Use let instead of def for the first definition of a throw and insert the remaining def calls inside let :

 (let [root-dir (File. "projects/my-project") src-folder (File. root-dir "src")] (def lib-folder (File. src-folder "lib")) (def dist-folder (File. src-folder "bin"))) 

The src-folder local binding value is discarded after the let evaluation, leaving only the available lib-folder and dist-folder vars for the rest of the program.

+5
source

If they were defn (i.e. functions), you can simply use defn- to create a private function.

For def you need to explicitly specify them as private:

 (def ^:private src-folder (File. "src")) 
+4
source

All Articles