Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private def in clojure/clojurescript

In Clojure and clojurescript you can have a private version of defn called defn-, but how do you do the same for def, as def- doesn't seem to be included?

like image 777
yazz.com Avatar asked Dec 07 '13 16:12

yazz.com


4 Answers

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.

like image 129
juan.facorro Avatar answered Oct 18 '22 00:10

juan.facorro


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.

like image 43
Mik Avatar answered Oct 17 '22 23:10

Mik


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

(defmacro def- [item value]
  `(def ^{:private true} ~item ~value)
)
like image 15
Tom Parker-Shemilt Avatar answered Oct 18 '22 00:10

Tom Parker-Shemilt


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.

like image 10
leeor Avatar answered Oct 18 '22 00:10

leeor