我很好奇为什么这会起作用(正如我在阅读 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/