beginners

MVitalii 2025-11-12T16:25:45.892089Z

Hi. Could someone explain reasons why 0, empty strings or empty collections are considered truthy?

2025-11-13T19:02:56.358089Z

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)
:OK

dpsutton 2025-11-13T19:08:45.662929Z

haha. this one is hilarious to me.

[(if (Boolean. "false") :wut :OK)
 (if (Boolean/parseBoolean "false") :wut :OK)]
but that’s a heck of a footgun

2025-11-14T01:17:46.555419Z

root 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.

2025-11-17T21:51:02.445469Z

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

😱 1
oskarkv 2025-11-14T12:33:31.126079Z

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.

MVitalii 2025-11-24T13:19:27.946669Z

@noisesmith same story, I was working with python for years and just switched to clojure and this very same issue is frustrating

dpsutton 2025-11-12T16:26:11.634109Z

are you dealing with clojurescript?

dpsutton 2025-11-12T16:26:22.045219Z

ah. no i see what you mean. Why they are truthy.

dpsutton 2025-11-12T16:26:52.664189Z

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

👌 1
⭐ 3
dpsutton 2025-11-12T16:27:38.272169Z

So i think a better question to send back to you is, “justify why 0 should be not truthy”

teodorlu 2025-11-12T16:38:26.809629Z

This is called "nil punning". Which is awesome! This explanation is nice: https://ericnormand.me/podcast/what-is-nil-punning

🙇 1