Clojure 插入符号作为符号?
所以我认为命名一个计算指数 ^
的函数是一个好主意,但插入符号实际上做了一些特殊的事情,因为 Clojure REPL 在评估 ' 时会生成错误^。谷歌搜索主要给了我这个,所以我想知道Clojure中插入符的实际用途是什么。
(另外,到底有可能命名一个函数 ^
吗?)
So I thought it would be a nice idea to name a function that calculates the exponential ^
, but it seems like the caret actually does something special, as the Clojure REPL generates an error when evaluating '^
. Googling mostly gave me this, so I was wondering what the actualy use for the caret in Clojure is.
(Also, would it be possible after all to name a function ^
?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
^
是“元字符”,它告诉阅读器添加以 < 开头的符号code>^ 作为下一个符号的元数据(前提是它是实现 IMetas 的东西)您可以通过查看事物的
meta
来了解有关 clojure 在幕后如何工作的很多信息,例如实例函数:这经常用于类型提示
打开反射警告通常是个好主意
(设置! *warn-on-reflection* true)
然后添加类型提示,直到警告消失。如果没有这些,Clojure 将在运行时查找函数操作数的类型,这会省去您为类型而烦恼的麻烦,但代价很小。PS:我下一个最喜欢的读者字符是“dispatch”字符
#
,接下来它非常值得学习:)PPS:这在 clojure 1.2.x 与 clojure 1.3.x 中是不同的
在 Clojure 1.2.1 中,当您使用元字符时,元数据不会组成:
在 1.3 中,它“做正确的事情”,并且关键字是选项而不是“标签”:
^
is "the meta character" it tells the reader to add the symbol starting with^
as metadata to the next symbol (provided it is something that implements IMetas)You can learn a lot about how clojure works under the hood by looking at the
meta
of things, for instance functions:this is very often used for type hints
it is generally a good idea to turn on reflection warnings
(set! *warn-on-reflection* true)
and then add type hints until the warnings go away. without these Clojure will look up the type of the function operands at run-time, which saves you the trouble of fussing with types though at a slight cost.PS: My next favorite reader character is the "dispatch" character
#
, it is well worth learning about it next :)PPS: this is different in clojure 1.2.x vs clojure 1.3.x
in Clojure 1.2.1 metadata does not compose when you use the meta-character:
and in 1.3 it "does the right thing" and also keywords are options instead of "tags":
不幸的是,在我看来,你的问题的答案是否定的。在 Clojure 中,您不能命名函数
^
。我在 REPL 中尝试了以下操作:
这似乎意味着您可以使用反斜杠转义克拉 (
^
)。但是,如果我尝试使用\^
作为名称来声明函数,则会收到一条错误消息:The same code works with a Regular text name:
I will be happy if people with better Clojure-fu比我的可以证明我错了! :)
It seems to me that the answer to your question is, unfortunately, no. In Clojure, you cannot name a function
^
.I tried the following in the REPL:
This seems to imply that you can escape the carat (
^
) with a backslash. However, if I try to declare a function using\^
as a name then I get an error message:The same code works with a regular text name:
I would be delighted if someone with better Clojure-fu than mine could prove me wrong! :)