3 ms·
This strikes me as a worst-of-both-worlds approach. I've come to prefer explicit booleans vs. C-style truthiness, but if you're going to limit falseness so stri
by jerf 16y ago
This strikes me as a worst-of-both-worlds approach. I've come to prefer explicit booleans vs. C-style truthiness, but if you're going to limit falseness so strictly you ought to make it a type error to have anything but a Boolean in the if statement. Even a run-time type error would be fine, Erlang mostly works that way ("true and 0" results in a bad argument exception, unfortunately the "if" construct is less picky). Making an interface such that any object can declare a reasonable definition of truthy and therefore the if can take anything seems like another valid choice, I've had good success with that in Python. This intermediate approach seems to combine the badness of both approaches, not the goodness.
- dmoney 16y agoI remember reading about the interface for making a class act as a boolean the last time I messed with Python. I thought it was neat that you could make any object act as a boolean, function, whatever. I can't seem to find the docs for it though.
- danielparks 16y agoI believe this is what you're talking about: http://docs.python.org/py3k/reference/datamodel.html#object.__bool__ http://docs.python.org/py3k/reference/datamodel.html#object....
- dmoney 16y agoThat's the one. Thanks.
- kbd 16y agoI like Python's approach. Anything null, zero, or "empty" is false (+ False of course), but it won't do any type conversion, so you don't get cases like "0" being true. Edit: though I wish the standard library used its configurable truthiness a little more. I'd like a closed filehandle to evaluate to False, for instance.
- kbd 16y ago> so you don't get cases like "0" being true I meant to say false :(
- weavejester 16y agoClojure isn't unusual in how it handles nil and false. Scheme, Ruby and Lua all use the same system, and I'm sure they're not the only ones. I actually prefer this system in dynamically typed languages, as it seems to get the right balance between simplicity and practicality. A system that just uses explicit true and false values would result in more redundancy in conditionals, e.g. (if (not (nil? x)) ...) Compared to: (if x ...) On the other hand, a boolean system that's any more complex tends to be hard to remember. For instance, I could never remember whether a blank string was considered true or false in Python, and I certainly can't remember all of the Javascript edge conditions relating to booleans.
- jerf 16y agoWhile you can construct ugly-looking scenarios, ugly conditionals comes up way less often that you would think in languages that have a strict requirement for booleans in if statements. Usually they also have pattern matching; extensive use of "if" in a pattern matching language is a code smell. Your example is also sort of a degenerate case for Lisp syntax. (Hey, it can't all be upside with no downside.) In languages that actually have a syntax it is, again, not something that is anywhere near as bad as you might think. I generally only notice when I actually want to have 0 be the false case and there is something to be said for putting the == 0 there anyhow; someone once said explicit is better than implicit.
- adobriyan 16y agoWhat has _syntax_ to do with anything being discussed?
- weavejester 16y agoStatically typed languages generally need to have strict requirements for booleans, unless you have a weak type system like C. But for dynamically typed languages, the only one that I can think of with strict booleans is Erlang. Are you aware of any others? I'm also not convinced this is a special case for Lisp syntax. In Ruby, one often finds code like: hash[key] ||= value find_resource(key) or not_found And even if it is a special case, aren't we talking about how suitable this is for Clojure specifically?