Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You're explaining the implementation, which I understand. There are reasons behind everything in the infamous "Wat!" video. The inconsistency is in conceptual expectations: (0) is true, (not 0) is false, yet (0 == true) is false. These are unexpected "gotchas" familiar to experienced Lua programmers.

It's not a dealbreaker. It's just a Lua quirk that often surprises newcomers. I was pointing out that Squirrel uses more conventional boolean evaluation that script authors coming from other languages may be more comfortable with. If you're exposing a scripting API, the language you use is essentially a part of your user interface.



As long as (0) and (not 0) are opposites; and (1) and (not 1) are opposites, there is nothing wrong, inconsistent or surprising.

But I think I see what you are trying to say. You want the == operator to coerce its operands to the same type before comparing (like JavaScript's == operator), but Lua's == operator doesn't do that, it simply compares. And that's why other languages need an === operator and Lua does not.

(JavaScript, by the way is the inconsistent one in this regard: the == operator does coerce its operands, but if(something == true) doesn't do the same thing as if(something). Try it with an empty array)


Lua's behavior here is identical to Ruby's, where 0 is also a truthy value:

    $ irb
    irb(main):001:0> if 0 then puts "0 is truthy!" end
    0 is truthy!
    => nil
    irb(main):002:0> if (not 0) then puts "(not 0) is truthy" end
    => nil
    irb(main):003:0> if (0 == true) then puts "0 is equal to true" end
    => nil
Many things about many programming languages are surprising to newcomers. However, the rule is very simple (in Lua): In a boolean context, every value besides nil and false is treated as though it were true. It is absolutely consistent. (a == b) is not a boolean context, or else 2 == 3 would be true.


Are you confusing (1==true) with (1 and true)? The value 1 is a number while true is a Boolean. Why should apple==orange ever be true?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: