(Boolean. false) in Clojure

后端 未结 3 1250
隐瞒了意图╮
隐瞒了意图╮ 2020-12-11 17:11

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

相关标签:
3条回答
  • 2020-12-11 17:23

    You can find the explanation at http://clojure.org/special_forms#if.

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

    [...] All [...] conditionals in Clojure are based upon the same logic, that is, nil and false constitute logical falsity, and everything else constitutes logical truth, and those meanings apply throughout. [...] Note that if does not test for arbitrary values of java.lang.Boolean, only the singular value false (Java's Boolean.FALSE), so if you are creating your own boxed Booleans make sure to use Boolean/valueOf and not the Boolean constructors.

    Compare

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

    with

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

    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.

    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.

    0 讨论(0)
  • 2020-12-11 17:26

    I think the reason this happens is that Clojure's = uses Java's equals method. So (= x y) is like x.equals(y). So false gets coerced into (Boolean. false) in the comparison under the hood.

    Note that this does not mean that (Boolean. false) is false or that it is the 'same' as false, just that when false and (Boolean. false) are compared using the equals method they are considered to be equal.

    0 讨论(0)
  • 2020-12-11 17:38

    Don't ever and never call (Boolean. true) or (Boolean. "true"). Don't create any instance of Boolean class. Those two forms are really evil.

    This isn't an issue of Clojure, actually it is a Java's one.

    There are only two possible values for a boolean: true or false which are already provided by Java. The constructors give you the illusion that you can create a new instance of Boolean class that can behave as a Boolean but it won't.

    If you really want to create a Boolean instance from a String or a boolean, then use valueOf() method of Boolean class.

    (Boolean/valueOf "true")
    (Boolean/valueOf  true)
    

    Boolean.html#valueOf(boolean)

    0 讨论(0)
提交回复
热议问题