Hi. Could someone explain reasons why 0, empty strings or empty collections are considered truthy?
also, it's not just any false that is false, only Boolean/FALSE is false, and other Falses are actually true
user=> (if (Boolean. "false") :OK)
:OKhaha. this one is hilarious to me.
[(if (Boolean. "false") :wut :OK)
(if (Boolean/parseBoolean "false") :wut :OK)]
but that’s a heck of a footgunroot issue is that checking for the specific object Boolean/FALSE (the value of false) is cheaper than checking if a value is an instance of Boolean that has value false. so a bunch of clojure core code only works correctly if you provide that sentinel value.
for the day job I use python now, and the fact that I can't use if to check if a value is present in some location (because the value might be an empty collection or 0) is extremely annoying
I think the answer is that it is simpler if they are not considered truthy. If an empty collection was falsey, that would be a special case. I mean, if a collection were considered truthy unless it were empty (a special case that depends on the contents). I think it's better to just check with seq or empty?. I can see that if you would like for an empty collection to be considered falsey, it might seem annoying, but I bet it would be more annoying the other way around, to handle special cases.
@noisesmith same story, I was working with python for years and just switched to clojure and this very same issue is frustrating
are you dealing with clojurescript?
ah. no i see what you mean. Why they are truthy.
There’s a simple rule around truthiness. The list of thigns that are considered false is a very short list: nil and false. Everything else is truthy
So i think a better question to send back to you is, “justify why 0 should be not truthy”
This is called "nil punning". Which is awesome! This explanation is nice: https://ericnormand.me/podcast/what-is-nil-punning