Clojure 的点特殊形式怪异

标签 clojure clojure-java-interop

我很好奇为什么这会起作用(正如我在阅读 the documentation on the dot special form 后所期望的那样):

(map #(. % isInstance {}) [clojure.lang.IPersistentMap])

返回:

(true)

但这并不:

(. clojure.lang.IPersistentMap isInstance {})

我收到错误“没有匹配的方法:isInstance”。该形式与上面的 map 函数调用中的形式完全相同,但在 map 之外,它不起作用。为什么?

最佳答案

.(点)螺旋形式是其中最奇怪的。不确定我是否能很好地解释它,但让我们尝试一下。

docs :

If the first operand is a symbol that resolves to a class name, the access is considered to be to a static member of the named class. Note that nested classes are named EnclosingClass$NestedClass, per the JVM spec. Otherwise it is presumed to be an instance member and the first argument is evaluated to produce the target object.

强调我的。

因此,您遇到了第一种情况 (. clojure.lang.IPercientMap isInstance {}) - clojure.lang.IPercientMap 解析为类名和整个表达式假定是静态方法调用。

map 情况下,符号会被求值(强调的部分),在传递给匿名函数和整个表达式是对该类的实例方法调用。

因此,归结为这样一个事实:在一个地方,clojure.lang.IPercientMap 被用作引用类名的符号,而在另一个地方则被用作计算为类的符号目的。

另请参阅here :

Note that class names normally denote class objects, but are treated specially in certain special forms, e.g. '.' and new.

关于Clojure 的点特殊形式怪异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32266326/

相关文章:

clojure - 如何检测符号是否解析为 (var contains a) 宏?

file - 如何在 Clojure 中从文件中读取多个变量?

clojure - 当 fns 作为参数传递时,执行函数向量不起作用

Clojure 使用重载方法具体化 Java 接口(interface)

emacs - 如何评估 Emacs init.el 文件中的某些内容?

clojure - 数据查询性能改进

java - com.google.cloud.storage.StorageImpl 的 clojure java 互操作

Clojure 使用 Java JsonPath

java - Clojure 和 JavaFX 2——将多参数传递给 JavaFX 方法

clojure - 您如何为 Leiningen 配置专有依赖项?