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

Viewed 117

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?

1 Answers

'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 (eval 'False)). (Confusingly, Hy has a core function named eval that's different from Python's eval. In the future, you'll probably be expected to call it hy.eval instead of it being automatically imported.)

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

With a plain old bool, as in Python.

Related