Clojure中的var是作为对象存储的吗?

发布于 2025-01-14 07:06:33 字数 684 浏览 1 评论 0原文

Clojure中的var是作为对象存储的吗?

问题 1: 如果是,那么哪个类的对象?

问题 2: 在Java中,我们有存储在线程堆栈上的引用,它们引用存储在进程堆上的对象。 如何像 Java 中的对象一样解释 Clojure 中的 var。

编辑:

为了自己尝试第一个问题,我使用type/class来检查对象的类。

情况 1:

(def a 100)
(type a)

O/P: java.lang.Long

在这种情况下,很明显 aLong 类的对象。

但是,如何检查相同的功能。例如 -

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 vars 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不念旧人 2025-01-21 07:06:34

如果您对之前定义的某个 x 调用 (type x),那么您将使用 x。如果您想了解 var 的类型,则需要查看 var:

user=> (type #'a)
clojure.lang.Var

获取有关对象的信息的一种简单方法是使用 bean 函数

user=> (bean #'a)
{:watches {}, :validator nil, :public true, :threadBinding nil, :bound true, :dynamic false, :macro false, :class clojure.lang.Var, :rawRoot 42, :tag nil}

通过 parents 查找层次结构

user=> (parents (class #'a))
#{java.io.Serializable clojure.lang.IFn clojure.lang.Settable clojure.lang.IRef clojure.lang.ARef}

JVM 中的所有内容都是 Object 的后代(原始类型除外)。

If you call (type x) on some previously defined x, then you are using the value of x. If you want to find out about the type of the var, you need to look at the var:

user=> (type #'a)
clojure.lang.Var

A naive way to get information about object is the bean function

user=> (bean #'a)
{:watches {}, :validator nil, :public true, :threadBinding nil, :bound true, :dynamic false, :macro false, :class clojure.lang.Var, :rawRoot 42, :tag nil}

Finding out about the hierarchy via parents:

user=> (parents (class #'a))
#{java.io.Serializable clojure.lang.IFn clojure.lang.Settable clojure.lang.IRef clojure.lang.ARef}

Everything in the JVM is an Object-descendant (except the primitive types).

寄居人 2025-01-21 07:06:34

更新 - 太快输入原始答案


“Var”的概念内置于 Clojure 语言/编译器中。 Var 通常通过命名空间符号来访问。每个命名空间都像映射一样实现,其中映射键是 Clojure 符号,映射值是 Clojure Var 对象。

每个 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 Clojure Var objects.

Each Var object is a mutable object that points to an immutable Clojure "value" (eg 5, [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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文