Clojure中的var是作为对象存储的吗?
Clojure中的var
是作为对象存储的吗?
问题 1: 如果是,那么哪个类的对象?
问题 2: 在Java中,我们有存储在线程堆栈上的引用,它们引用存储在进程堆上的对象。 如何像 Java 中的对象一样解释 Clojure 中的 var。
编辑:
为了自己尝试第一个问题,我使用type
/class
来检查对象的类。
情况 1:
(def a 100)
(type a)
O/P: java.lang.Long
在这种情况下,很明显 a
是 Long 类的对象。
但是,如何检查相同的功能。例如 -
case 2:
(type identity)
O/P:
clojure.core$identity
因此,在 case 2
中,type
返回命名空间而不是类。
如何获取班级?
Is var
in Clojure is stored as an object?
Question 1:
If yes, then object of which class?
Question 2:
In Java, we have references stored on the thread stack and the they refer to the objects stored on process heap.
How to interpret var
s in Clojure like the analogy of objects in Java.
EDITS:
To try the first question by myself, I used type
/class
to check the class of the object.
Case 1:
(def a 100)
(type a)
O/P:
java.lang.Long
In this case, it is clear that a
is an object of Long class.
But, how to check the same thing for functions. For example -
case 2:
(type identity)
O/P:
clojure.core$identity
So, from the case 2
, type
returns the namespace instead of class.
How to get the class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您对之前定义的某个
x
调用(type x)
,那么您将使用x
的值。如果您想了解 var 的类型,则需要查看 var:获取有关对象的信息的一种简单方法是使用
bean
函数通过
parents 查找层次结构
:JVM 中的所有内容都是
Object
的后代(原始类型除外)。If you call
(type x)
on some previously definedx
, then you are using the value ofx
. If you want to find out about the type of the var, you need to look at the var:A naive way to get information about object is the
bean
functionFinding out about the hierarchy via
parents
:Everything in the JVM is an
Object
-descendant (except the primitive types).更新 - 太快输入原始答案
“Var”的概念内置于 Clojure 语言/编译器中。
Var
通常通过命名空间符号来访问。每个命名空间都像映射一样实现,其中映射键是 Clojure 符号,映射值是 ClojureVar
对象。每个
Var
对象都是一个可变对象,指向不可变的 Clojure“值”(例如5
、[1 2 3]
、{:a 1 :b 2}
等)。请参阅此文档源列表,尤其是“获取 Clojure”。有关详细信息,请查看 Clojure
namespace
及其 实现。Updated - typed original answer too quickly
The concept of a "Var" is built into the Clojure language/compiler. A
Var
is normally accessed via a namespaced symbol. Each namespace is implemented like a map, where the map keys are Clojure symbols and the map values are ClojureVar
objects.Each
Var
object is a mutable object that points to an immutable Clojure "value" (eg5
,[1 2 3]
,{:a 1 :b 2}
etc).Please see this list of documentation sources, esp "Getting Clojure". For the nitty gritty details, look into the Clojure
namespace
and its implementation.