Why (defn- ...) but (def ^:private? ...)

1,129 views
Skip to first unread message

Stig Brautaset

unread,
May 7, 2015, 10:12:39 AM5/7/15
to clo...@googlegroups.com

Is it because the def form can also be ^:dynamic?

At any rate, I did an attempt at my first macro to create a (def- ...) form, but it doesn't seem to work. Can you not attach metadata in a macro?

(defmacro def-
  "Why (defn- private-fn ...) but (def ^:private var ...)?"
  [sym & body]
  `(def ^:private ~sym ~@body))

;; => #'user/def-
user> (macroexpand '(def- blah "foo bar quux"))
;; => (def blah "foo bar quux")

Stig

Colin Yates

unread,
May 7, 2015, 10:35:48 AM5/7/15
to clo...@googlegroups.com

Have you tried with-meta?

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Herwig Hochleitner

unread,
May 7, 2015, 10:36:44 AM5/7/15
to clo...@googlegroups.com
2015-05-07 16:12 GMT+02:00 Stig Brautaset <sbrau...@gmail.com>:

Is it because the def form can also be ^:dynamic?

Hm, cognitect turned it down, because basically "if you go down this road, why stop with def- ? and before you know, you've got a core namespace full of def*-" 

At any rate, I did an attempt at my first macro to create a (def- ...) form, but it doesn't seem to work. Can you not attach metadata in a macro?

You can, but it works differently than in normal source. Remember, that ^ attaches metadata the literal, that you annotate. so ^:private ~sym is the symbol ~sym with a private metadata key. The quasiquoting, however, rebuilds the form and inserts the sym parameter in place of the ~sym form. So to attach the metadata to actual def'ed symbol, you need to attach it to the output, the macro generates, like this: `(def ~(with-meta sym {:private true}) ...)

adrian...@mail.yu.edu

unread,
May 7, 2015, 10:39:40 AM5/7/15
to clo...@googlegroups.com
Reader macros are evaluated at read time, not macroexpansion time. Read time happens during reading - the process in which the characters in a file are read into the Lisp forms Clojure supports. Therefore ^:private is not syntax which can be manipulated in a macro, because by the point the macro is evaluated that form is no longer present. 

Check out the source for defn- (you can find it in a REPL by calling (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))

(defmacro def-
  "same as def, yielding non-public def"
  ([name] `(def- ~name nil))
  ([name expr]
   (list `def (with-meta name (assoc (meta name) :private true)) expr)))
  

Stig Brautaset

unread,
May 8, 2015, 10:57:56 AM5/8/15
to clo...@googlegroups.com
Hi Herwig,


On Thursday, 7 May 2015 15:36:44 UTC+1, Herwig Hochleitner wrote:
2015-05-07 16:12 GMT+02:00 Stig Brautaset <sbrau...@gmail.com>:

Is it because the def form can also be ^:dynamic?

Hm, cognitect turned it down, because basically "if you go down this road, why stop with def- ? and before you know, you've got a core namespace full of def*-" 

And that is a fine justification :-) I was just curious.
 

Can you not attach metadata in a macro?

You can, but it works differently than in normal source. Remember, that ^ attaches metadata the literal, that you annotate. so ^:private ~sym is the symbol ~sym with a private metadata key. The quasiquoting, however, rebuilds the form and inserts the sym parameter in place of the ~sym form. So to attach the metadata to actual def'ed symbol, you need to attach it to the output, the macro generates, like this: `(def ~(with-meta sym {:private true}) ...)

Thank you for this explanation, it makes sense now!

Stig

Leon Grapenthin

unread,
May 8, 2015, 12:26:27 PM5/8/15
to clo...@googlegroups.com
Do you have a link to the discussion? Is this really the reasoning? 

I find it insufficient, because there aren't many defs supporting private :

def, defn, defmacro and definline if I am remembering correctly.

I am kind of tired of writing def ^:private. def- would really be nice to have. defmacro- as well. 

Is there a JIRA ticket about this?

Andy Fingerhut

unread,
May 8, 2015, 12:54:50 PM5/8/15
to clo...@googlegroups.com
I am not aware of any JIRA ticket for this, but there could be one lurking there somewhere.

Here is a link to a previous discussion about it on this group: https://groups.google.com/d/msg/clojure/r_ym-h53f1E/y1S31QXNhcAJ

Andy

--
Reply all
Reply to author
Forward
0 new messages