Marcus Junius Brutus
Marcus Junius Brutus

Reputation: 27286

Clojure series of defs but don't expose to the outer scope all of them

At the head of my clojure file I have a series of defs, some of which are only stepping-stone defs, i.e. are not meant to be used further down 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's the right way to do that in Clojure?

Upvotes: 4

Views: 61

Answers (2)

Alex
Alex

Reputation: 13961

Use let instead of def for the first throw-away definition, and nest the remaining def calls inside the 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 value of the src-folder local binding is discarded after the let is evaluated, leaving only the lib-folder and dist-folder vars accessible to the rest of the program.

Upvotes: 5

opqdonut
opqdonut

Reputation: 5159

If they were defns (i.e. functions) you could simply use defn- to make a private function.

For defs you have to explicitly set them as private:

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

Upvotes: 4

Related Questions