Lua "== true equals" "== false

I find it hard to understand how the expressions ""==true and ""==false are evaluated as false .

The following is done in the lua and ilua interpreter:

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

Or follow these steps:

 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 executed:

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

Result (seemingly inconsistent)

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

As expected in the Lua FAQ, which states

C-like languages โ€‹โ€‹consider 0 the equivalent of false, but this is not true for Lua. Only explicit false or zero is equivalent to false. when in doubt, make the condition explicit, for example. if val == nil then ... end if the value is actually not logical.

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

+6
source share
4 answers

All Lua values โ€‹โ€‹when used as logical variables are calculated as true except nil and false. This does not mean that values โ€‹โ€‹that match true are true. If you want to convert the value of v to Boolean, use not not v .

+21
source

The type "" is a string, not a logical one, so it is not equal to either true or false .

To be more general, when Lua compares two values, it first checks its type, if the type mismatch, Lua considers that the two values โ€‹โ€‹are not equal at once.

When used as a control expression, the only false values โ€‹โ€‹in Lua are false and nil , all others are evaluated as true. Some popular confusions include the number 0 , the empty string "" , the string "0" , all of which are true values. Note again that false and nil not equal, because they are different types.

So back to the example in the code

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

Lua checks if "" false or nil , since it is not, then Lua treats this condition as a true value.

+11
source

Disclaimer: I have no experience with lua, this is a reasonable assumption

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

+5
source

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

It seems that Lua considers comparisons of comparisons using == and comparisons made in control structures ( if , while , for , etc.) differently.

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

A control structure condition expression can return any value. Both false and zero are considered false. All values โ€‹โ€‹are non-zero and false are considered true (in particular, the number 0 and the empty string is also true).

It seems to match what you see. I.e:

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

Because the comparison operator seems to check the type and value.

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

 if "" then print "Hello world!" end 

Hello world! will be printed Hello world! , because the empty string is different from nil and false , and therefore evaluates the true value.

+1
source

Source: https://habr.com/ru/post/955767/


All Articles