hy

Why is it that '0 is false, but 'False is true?


I was playing around with symbols and was surprised to see that:

hy 0.18.0 using CPython(default) 3.7.3 on Linux
=> (bool '0)
False
=> (bool 'False)
True
=> 

Is that a design decision? What is the best way to represent boolean values on Hy?


Solution

  • '0 isn't a symbol; it's a HyInteger, which inherits from int and behaves like an int in many ways. In particular, it uses int's __bool__ method.

    'False is indeed a symbol (HySymbol), but most operations on a symbol, including bool, don't try to evaluate the symbol. Instead, they treat it like a string. At least for the time being, HySymbol inherits from str. So, bool on any nonempty symbol returns True. For the same reason, (+ 'x 'y) returns the string "xy" even if you've set the variables x and y to numbers. If you want to Booleanize the value of a variable represented by a symbol, rather than the symbol itself, say (bool (hy.eval 'False)).

    What is the best way to represent boolean values on Hy?

    With a plain old bool, as in Python.