Why end variables with hash symbol in macros?

2019-04-29 13:53发布

问题:

I was looking clojure.core's macro implementation of and and I noticed that some of the let bindings in this source file's macros end their variable name with and octothorpe (#).

Upon further inspection with the following code...

(defn foo# [] 42)
(foo#) ; => 42

...I realized that octothorpe is just a valid symbol (at least when included on the end).

So, my question is, why do these core macros end their binding symbols with a hash character? Is there some specific implied meaning or convention I am missing here?

回答1:

The # at the end of a symbol is interpreted specially by the reader as a shortcut for gensym.

(gensym "foo")
;=> foo3

(defmacro hygienic []
  `(let [foo# 42] foo#))

(hygienic)
;=> 42

(macroexpand '(hygienic))
;=> (let* [foo__1__auto__ 42] foo__1__auto__)