Fork me on GitHub
#clojure-dev
<
2018-12-19
>
Marc O'Morain11:12:44

@andy.fingerhut I’ve been bitten in the past by the unexpected behaviour of Boolean/valueOf and Clojure branching logic – I wonder if it impacts on equality, too.

Marc O'Morain11:12:00

In my specific case, it turns out that grpc-java creates default values for unspecified message fields using the string constructor, which results in unspecified boolean fields being created with (Boolean/valueOf "false"), which is truthy in some situations in Clojure.

bronsa11:12:11

curious if you have an example of where this happens

bronsa11:12:41

I remember discussions about this, happening 5-6 years ago but I've since forgotten how the issue manifests

mfikes13:12:05

One way you can run into this is via Java serialization. A concrete example is when using Spark and you end up looking at a deserialized Boolean.

alexmiller13:12:58

Clojure requires that you use the Boolean cached values

alexmiller13:12:42

Otherwise a Boolean constructed any other way is treated as true, including a boxed false

bronsa14:12:27

but doesn't Boolean/valueOf always return the cached value?

bronsa14:12:37

IIRC the only issue was with using e.g. (Boolean. "false")

alexmiller15:12:45

correct, that’s what I’m talking about

alexmiller15:12:30

maybe Marc really meant Boolean/parseBoolean? dunno

andy.fingerhut16:12:09

At least some of the issues about boolean constructors are described on the http://ClojureDocs.org if page here: http://clojuredocs.org/clojure.core/if

andy.fingerhut16:12:23

In a Clojure 1.10 REPL at least I see (= false (Boolean. false)) return true, which looks reasonable. It is using the (Boolean. false) in an if or cond condition that will bite you.

Marc O'Morain22:12:22

> maybe Marc really meant Boolean/parseBoolean? dunno Yeah, I can’t remember the exact call used to construct the Boolean – I was debugging through https://github.com/protocolbuffers/protobuf trying to find out what was going on, which lead me to the note about if that Andy linked to.

andy.fingerhut01:12:20

I haven't tried it myself, but as it says on that http://ClojureDocs.org page, even Java documentation anti-recommends the use of (Boolean. false) values, so if Google's protobuf implementation in Java returns data structures containing such values, it sounds like an enhancement request might be in order.