7 ms·
Why is zero not falsy in Clojure? (2015)
- Slackwise 5y agoClojure, like Ruby, does falseyness correctly in that there's only 2 cases of false: nil and false. Everything else is true. This is easy to reason about, and it allows for nils to "flow" through your logic (especially with nil-punning where in the context of collections, nil is also an empty sequence).
- tialaramex 5y agoSomething even easier to reason about: Only true is true, and only false is false!
- eropple 5y agoI disagree - presence/absence allows reuse of clear and terse operators, reduces the need for an extra null coalescence operator when logical or can do the job as well, and enables better developer experience in places like options hashes where "not set" always means the same thing as "false".
- happytoexplain 5y agoI think modern languages have demonstrated that the dev UX difference between allowing "falsey"/"truthy" and not allowing it can be reduced to be quite small. Meanwhile, consider that the bugs introduced by implicitly considering things false/true are arguably a type of "bad dev UX".
- eropple 5y agoI can't think of a case, personally, where conflating false and nil/null/undefined has created a bug. I can think of many for, like, JS treating the empty string or zero as false, but not for specifically null/undefined. Can you provide an example?
- capableweb 5y agoYou're talking about hard true/false, in which you are correct. The person you're replying to is talking about "Truthy"/"Falsy", which is different than the hard true/false you're talking about.
- happytoexplain 5y agoI read the parent as, slightly sarcastically, saying "it's easiest to reason when the concept of truthy/falsy is completely absent".
- deleted 5y ago[deleted]
- samatman 5y agoAdd Lua to the languages which chose to be sensible about this.
- ErikCorry 5y agoAnd Toit. Only null and false are falsey.
- normie3000 5y agoColloquially, “nil” and “zero” are the same thing. Presumably not the case in clojure.
- ARandomerDude 5y agoTrue, however in software there are many cases in which they're different. Simple example: How many checked luggage items do you have? nil means the user hasn't answered the question 0 means they saw the question and don't have any checked luggage.
- the_only_human 5y agoin your example, the end result is the same. the user is not interested in luggage.
- happytoexplain 5y agoThe difference between an answered and unanswered question is absolutely not the same. Nil means the user has not told us how much luggage they have. 0 means the user has 0 luggage. We can assume a lack of response means zero, or we can mark the field as required, in which case the UI needs to prompt the user for a response in the nil case but not the 0 case. This is an extremely simple, common example. There are many cases where nil is not equivalent to 0 semantically or logically.
- gryn 5y agono that's you assuming since that part was not filled.
- happytoexplain 5y agoI do not agree that treating nil (or zero, or an empty collection) as false is "correct".
- IshKebab 5y agoThat's doing it almost correctly. The correct way to do truthiness is not to do it at all. `nil` should not be automatically coerced to `bool`.
- SeanLuke 5y agoThis is a baloney C-biased question. The real question is why nil is falsey (ALONG with false) in Clojure. To my mind this is an incredibly boneheaded mistake in a modern language. Common Lisp can be excused as it is old and has an even older ancestry. nil has been false in common lisp and its ancestors since forever, but this is an accident of history, and nobody claims it to be a good idea, as it is a source of many bugs. And indeed some lisp functions have to have additional gizmos added to them to work around it. Scheme fixed this with real true and false constants. And then clojure, um, decided to combine the worst of both worlds, by making TWO things be false.
- pacaro 5y agoScheme's explicit #f is false and everything else is truthy takes some getting used to, but conversely look at Tcl where boolean values are any numeric value (0 is falsy) or the strings: true, false, yes, no, on, off
- dgb23 5y agoFalse and nil being two distinct values isn't Clojure's fault though. Or not directly. There are a bunch of trade offs that Clojure makes to have very direct interop with its host languages.
- SeanLuke 5y agoClojure sits on the JVM. Why can't false be false and nil be NULL? I know of no other JVM-based lisp system which makes this "tradeoff". Indeed Kawa, which is rather faster than Clojure in my experience, does not.
- dgb23 5y agoClojure's nil on the JVM and Java's null are the same value. Are we misunderstanding each other, or am I missing something?
- deleted 5y ago[deleted]
- lmkg 5y agoThe framing of the original question is ill-posed. It says that C treats 0 as falsey in a boolean context, but early versions of C did not have booleans. Thinking that C treats 0 as false is looking at it from a perspective deeply influenced by modern languages. The IF statement in C tests for zero, just like many assembly languages. The idea of "false" is a higher-level abstraction that was successfully back-ported to C. My own thoughts is that what values should be falsey in a language depends on what your language is supposed to manipulate. C is a language designed for mucking about with machine data: bits, numbers, and pointers. And those are the data types that can be sensibly used in a boolean context in C. The Lisp family is designed for processing lists (it's in the name), and lists are the only type that some Lisps let be falsey (although many don't even allow that). It seems sensible to me that a fundamentally object-oriented language would treat a null object as falsey, or the empty string in a language for text processing. I dislike it when multi-paradigm languages have many falsey values.
- mikepurvis 5y agoIn my opinion if a language has False/None/Null/Nil as first class values, then the only sane path is for everything else to be True: the number zero, NaN, empty string, empty list, empty set, etc. All of those should evaluate to True.
- zasdffaa 5y agoYeah... ISWYM and it sounds good, then again it confuses the boolean concept with the list concept, the set concept etc. etc. etc. Doesn't mean it isn't useful, and maybe there's very little risk of mistakes but still.
- mikepurvis 5y agoIf every list or set or dict evaluates to true, even empty ones, what is there to confuse? Or is the argument that non-bool types shouldn't coerce to a bool at all, so that you're force to always explicitly call a bool-returning method, like "if my_list.empty() then" type of thing? In any case, the next thread down is making the same point, and specifically calling out languages that do exactly this and get it right: Clojure, Lua, Ruby, and others.
- gavinpc 5y agoSee also this question: https://stackoverflow.com/questions/5830571/why-did-father-of-clojure-say-that-schemes-true-false-are-broken https://stackoverflow.com/questions/5830571/why-did-father-o... Which leads to this link: https://groups.google.com/g/clojure/c/OnagUrQZ1NE/m/Uwm8fvakuTQJ https://groups.google.com/g/clojure/c/OnagUrQZ1NE/m/Uwm8fvak... Where Rich comments on the thinking behind this. Also see https://clojure.org/reference/lisps https://clojure.org/reference/lisps , which compares Clojure with other Lisp dialects on this and other points
- weatherlight 5y agoAll values / types in Elixir apart from nil and false are truthy, like Clojure and Ruby. Pattern matching in Elixir is so powerful, why would you even bother?
- rubyist5eva 5y agoBecause not every language is based on C semantics. Ruby also does this, nil and false are the only falsey values. Funny enough, NULL::bool = false in postgres is NULL - because of course it is.
- StefanKarpinski 5y agoInspired by this I wrote a post on the Julia discourse forum about the arbitrariness of truthiness in programming languages: https://discourse.julialang.org/t/on-the-arbitrariness-of-truth-iness/81381 https://discourse.julialang.org/t/on-the-arbitrariness-of-tr.... Julia requires actual `true` or `false` values in conditionals. Some people are annoyed coming from languages where they can test if a list is non-empty without having to write `isempty(a)`. But I have no regrets and this is a great example of why. Languages cannot agree on truthiness because it's arbitrary and just becomes a set of random rules you have to remember and which causes nasty bugs if you get them wrong.
- brundolf 5y agoFalsey 0 in JavaScript is a big source of problems and is often linted against (forcing explicit checks, which then make code a bit uglier, but at least you won't get bitten by the case where 0 gets treated like null instead of a number) Count yourself lucky Edit: Thinking about it, this is probably a frame-shift from static to dynamic languages, because in the latter any value can be nil/null at any time. Whereas in C, C++, and even Java etc, unboxed numbers are always numbers. So coming from those you may not be used to thinking about the case where you have a nullable number and mean to check for null and accidentally also check for 0. But in dynamic languages like Clojure, it's a common situation to have a nullable number.
- smegsicle 5y agozero being false is fine in statically typed languages like C, because there's still only one false value available, and you wouldn't be checking for it if you weren't expecting zero to be false in dynamic languages, wanting to take advantage of brevity tricks with number zero or empty list or empty string being falsy can seem like a good idea, but gets confusing fast- different languages are going to have different falsy values, and you can easily get caught off guard when legitimately wanting to accept an 'empty' value common lisp has NIL as its definition of the end of a list, which ends up working out fine since lists are so deeply embedded in the language design that it's hard to forget that they're there (any other kind of object is 'truthy', including any size of regular array) scheme has a separate #f that is taken as the only falsy value, which is probably also fine python and javascript are a mess, with different kinds of 'empty' or 'nothing' values being falsy whether you want them to be or not, which is the kind of helpfulness we don't need- where evaluating in boolean context means "if this value is present but not 'empty', in a way that is dependent on the objects type, and may change over time"
- deleted 5y ago[deleted]
- mc4ndr3 5y agoA good, illustrative case for why falsiness, of any kind, leads to disaster. Boolean operations should require explicit mapping to boolean types. Make the intent clear every time, instead of requiring the programmer to memorize umpteen different rules.
- tmm84 5y agoI agree that using a boolean or calling a function that returns a boolean is the best case for readable code. I think that familiarity with a language leads to using the falsy logic rules without explicit boolean logic. When script programming falsy logic can be broad and useful but for software that needs to be robust explicit boolean logic can not be beat.
- dgb23 5y agoThere's no disaster as far as I know. This is a old, well understood Lisp idiom. Clojure specifically has a ton of macros, functions and patterns around this concept, such as if-let, when-let, some->, looking up stuff in a map, just to name a few, much this goes under the umbrella of "nil-punning" and "logically true values" etc. Caveats and boundaries: It is typically clear whether something can return a nil or accepts a nil value one versus when it doesn't. A good example would be string methods and other Java interop, where you will make sure you don't pass in a nil value, you are crossing language boundaries here so to speak. Another boundary of nil usage would be sequences, which will not evaluate to falsy, even if empty, so you explicitly would call seq or empty? on them. The bottom line is that you are very aware of nil values and their falsiness in Clojure and that you program with them rather than against them.
- ramesh31 5y ago>A good, illustrative case for why falsiness, of any kind, leads to disaster. Yup. It took a decade of ES updates to overcome these issues with Javascript. The falsy zero in JS has probably created more bugs in aggregate than any other language feature in history.
- samatman 5y agoHow about two rules, or if you absolutely insist, three? "Values are falsey if they are `nil` or `false`, otherwise they are truthy". It's hard for me to map this rule to 'disaster', either in principle or in practice. It means that `if number then` does what you expect, if you expect that maybe number exists, maybe it doesn't.