本文介绍了(布尔,FALSE)Clojure中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据,Clojure中唯一的谎言是。事实上,令人惊讶的是,(布尔假)一点不假:

According to http://hyperpolyglot.org/lisp, the only falsehoods in Clojure are false and nil. Indeed, surprisingly enough, (Boolean. false) is not false:

user=> (if (Boolean. false) 1 2)
1
user=> (not (Boolean. false))
false
user=> (false? (Boolean. false))
false

在另一方面,它在某种程度上的假:

On the other hand, it somehow is false:

user=> (class false)
java.lang.Boolean
user=> (= false (Boolean. false))
true

这是比较常理。是否有原因的行为,或者是它只是忽略了?做类似的事情发生在其他的Clojure的Java数据类型?它的工作原理相同的ClojureScript和ClojureCLR或者是特定于JVM版本?做其他JVM的Lisp,像川或ABCL,显示出类似的行为(如果他们使用Java布尔值的话)?

This is rather counterintuitive. Are there reasons for this behaviour or was it simply overlooked? Do similar things happen to other Java data types in Clojure? Does it work the same in ClojureScript and ClojureCLR or is it specific to the JVM version? Do other JVM Lisps, like Kawa or ABCL, show similar behaviour (if they use Java booleans at all)?

推荐答案

您可以在这里找到。

这是很好的阅读全款,但这里的关键位摘录,加上强调:

It's good to read the whole paragraph, but here's the crucial bit excerpted, emphasis added:

[...]所有[...] Clojure中的条件语句是基于同样的逻辑,那就是构成的逻辑谬误,一切事物构成的逻辑真理,而对于整个含义适用。 [...]的注意如果不测试java.lang.Boolean的任意值,只有奇异值false(Java的Boolean.FALSE)所以,如果你正在创建自己的盒装布尔确保使用布尔/的valueOf而不是布尔构造函数。

比较

System.out.println(Boolean.valueOf(false) ? true : false);  // false
System.out.println(new Boolean(false)     ? true : false);  // false

user=> (if (Boolean/valueOf false) true false)
false
user=> (if (Boolean. false) true false)
true

因此​​,(布尔。FALSE)既不是也不,就像(对象)既不是也不。正如@Chiron指出,这是不好的做法,无论如何使用它。

Thus, (Boolean. false) is neither nil nor false, just as (Object.) is neither nil nor false. And as @Chiron has pointed out, it's bad practice to use it anyway.

至于(= FALSE(布尔假)。)是真实的,我觉得@噜比的解释是现货:由于 = 依赖于Java的等于方法,条件的Clojure中的特殊语义不适用,和布尔平等是因为它是在Java中。

As for (= false (Boolean. false)) being true, I think @looby's explanation is spot on: Since = relies on Java's equals method, the special semantics of conditionals in Clojure don't apply, and boolean equality will be as it is in Java.

这篇关于(布尔,FALSE)Clojure中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 23:35