Why end variables with hash symbol in macros?

送分小仙女□ 提交于 2019-12-07 04:47:13

问题


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__)


来源:https://stackoverflow.com/questions/25514362/why-end-variables-with-hash-symbol-in-macros

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!