Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why end variables with hash symbol in macros?

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?

like image 287
mjgpy3 Avatar asked Aug 26 '14 20:08

mjgpy3


1 Answers

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__)
like image 142
schaueho Avatar answered Sep 20 '22 10:09

schaueho