Private def in clojure/clojurescript

You have to add the :private true metadata key value pair.

(def ^{:private true} some-var :value)
;; or
(def ^:private some-var :value)

The second form is just a short-hand for the first one.


It's worth mentioning, that currently it's not possible to have a private def (and defn) in ClojureScript: https://clojurescript.org/about/differences (under "special forms")

Compilation won't fail and but the def will stay accessible.


If you want a def-, here's how to implement it

(defmacro def- [item value]
  `(def ^{:private true} ~item ~value)
)

This google group post has a discussion about this topic. Apparently the request has been considered. According to one of the responses, defn- was deemed to not be a good idea and decided not to perpetuate it with def and others.


Here's how to implement def-:

(defmacro def-
  "same as def, yielding non-public def"
  [name & decls]
  (list* `def (with-meta name (assoc (meta name) :private true)) decls))

This code is very similar to that of defn-, which you can look up using (clojure.repl/source defn-):

(defmacro defn-
  "same as defn, yielding non-public def"
  {:added "1.0"}
  [name & decls]
  (list* `defn (with-meta name (assoc (meta name) :private true)) decls))