8
votes

I'm having trouble understanding how the expressions ""==true and ""==false both evaluate to false.

Trying the following in the lua interpreter and ilua result in the same output:

> =""==true
false
> =""==false
false

Or executing the following:

print(""==true)
print(""==false)
print(""==nil)

Outputs

Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
false
false
false
> 

Another example:

> =""~=true
true
> =""==false
false

When the following code is run:

if "" then -- if ""==true
    print "was true"
end 

if not "" then -- if ""==false
    print "was not true"
end 

The output is (seemingly inconsistently)

Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
was true
> 

As expected per the Lua FAQ which states

C-like languages regard 0 as equivalent to false, but this is not true for Lua. Only an explicit false or nil are equivalent to false. When in doubt, make the condition explicit, e.g. if val == nil then ... end unless the value is actually boolean.

How can a value be not equal to true,false or nill?

4
Well, it seems just like "" is an empty String. So, it is not true, not false and neither nill. - Minoru
I don't see why it should? - Absurd-Mind
I think the problem is I've assumed strings would have truthy/falsy values. - HennyH
@HennyH, but you quoted the documentation that says they do not. So why are you still surprised by it? - finnw
By the way @HennyH, you're using Python usually, right? >>> ""==True False >>> ""==False False >>> ""==None False :) - catwell

4 Answers

27
votes

All Lua values when used as Booleans evaluate to true, except nil and false. This does not mean that values that evaluate to true are equal to true. If you want to convert a value v to Boolean, use not not v.

12
votes

The type of "" is string, not boolean, so it's not equal to either true or false.

To be more general, when Lua compares two values, it tests their type first, if the type mismatch, Lua thinks the two values as not equal immediately.

When used as control expression, the only false values in Lua are false and nil, everything else is evaluated as true value. Some popular confusions include the number 0, the empty string "", the string "0", they are all true values. Note again that false and nil are not equal because they are different types.

So back to the example, in the code

if "" then -- if ""==true
    print "was true"
end 

Lua tests if "" is false or nil, since it's neither, then Lua treats the condition as true value.

4
votes

Disclaimer: I have no experience with lua, this is an educated guess

This is probably because "" is an empty string, so Lua probably evaluates it as a string with length of zero. Since it's a valid object it's not going to be equal to true, false, or nil, it'll be equal to a string with length zero.

2
votes

Disclaimer: the only thing I know about Lua is that I don't know anything about Lua.

It appears that Lua treats equality comparisons using == and comparisons done in control structures (if, while, for, etc.) differently.

According to the Lua 5.1 manual (section 2.4.4, Control Structures),

The condition expression of a control structure can return any value. Both false and nil are considered false. All values different from nil and false are considered true (in particular, the number 0 and the empty string are also true).

It seems like this is consistent with what you are seeing. That is:

"" == false => false
"" == true => false
"" == nil => false

Because the comparison operator seems to be checking the type and the value.

However, if you use the variable in a conditional in a control structure, the behavior is slightly different. That is,

if "" then
    print "Hello world!"
end 

Will print Hello world!, because the empty string is different from both nil and false, and thus evaluates to a truthy value.