为什么在宏中用哈希符号结束变量? [英] Why end variables with hash symbol in macros?

查看:108
本文介绍了为什么在宏中用哈希符号结束变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看clojure.core的宏

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

...我意识到octothorpe只是一个有效的符号(至少在结尾处包含了该符号).

...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?

推荐答案

读者特别将符号末尾的#解释为

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

这篇关于为什么在宏中用哈希符号结束变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆