munificent / craftinginterpreters

Repository for the book "Crafting Interpreters"
http://www.craftinginterpreters.com/
Other
8.84k stars 1.04k forks source link

isTrue returns true for 0 (integer) #68

Closed gootik closed 7 years ago

gootik commented 7 years ago

It just seems weird to me that Lox treats !0 as false.

aggsol commented 7 years ago

0 is a valid number much like 1,2,3 or -13.37. Why should this singular number be false?

egordorichev commented 7 years ago

Because it indicates false. In c, c++, java and many other languages it is false.

aggsol commented 7 years ago

I know that, but this is "our" language we can form it with our own rationale. What is the reasoning that lox should treat 0 as false but not negative numbers or empty strings?

munificent commented 7 years ago

Ouch, this was a total oversight on my part. I'd originally documented Lox's truthiness rules in chapter 3. But I cut it out to keep the chapter shorter with the intention of moving it to chapter 9 ("Control Flow"). I forgot that it actually comes into play before that in "Evaluating Expressions".

Oops! I moved the chunk of prose into 7 explaining what truthiness means and what Lox's rules for it are and why. (Mainly, it follows Ruby and its simple.)

gootik commented 7 years ago

Sounds good, I wasn't aware this is how Ruby handles truthiness. Thanks.