2

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?

colidyre
  • 4,170
  • 12
  • 37
  • 53
λlover
  • 23
  • 3

1 Answers1

2

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

Kodiologist
  • 2,984
  • 18
  • 33