lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Am 26.07.2016 10:47, schrieb Dirk Laurie:
2016-07-26 8:55 GMT+02:00 Thomas Jericke <tjericke@indel.ch>:

As interesting statements like (a = a or 5) are there is
one thing you always have to remember while programming Lua, testing for
existence and testing for truth is the same unless you use explicit
comparison.
Actually, no. 'false' exists but does not test true. In fact, the type 'boolean'
was introduced in Lua starting at Lua 2.1 precisely because a value with
that property was needed.

I think you didn't understand me, probably because I wasn't clear enough.

If you use an if statement or use any boolean expression other than == you don't know whether you are testing for the value true of a boolean or for the existence of any value as you don't (always) know in beforehand if the type of that expression is boolean or not at programming time.

Usually it doesn't matter as you know what kind of value you are dealing with, it is only a problem if you write some code that handles booleans and other types.

Let's say you have a logic expression parser that returns either the result (true or false) or nil + error in case of syntax error.

local result, err = parse_logic(some_exp)
if result ~= nil then
    print("The result is ", result)
else
    error(err)
end

In such cases you have to explicitly compare to either nil, true or false as otherwhise it wouldn't do what you expect.
It isn't much of a problem as long as one is aware of such situations.
--
Thomas