lua-users home
lua-l archive

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


Alen Ladavac wrote:
In the original Lua, 0 was logically true, because the only logically false
value was nil. That made sense. But since the introduction of the boolean
type in Lua 5, there is a slight mess in what is actually logically false, causing me to reconsider on the idea of 0 being logically true.

Both nil and false are logically false now. But they are different, i.e
false~=nil . This is a bit awkward because you can't be sure about what does
"if foo then..." mean. Furthermore, this:

if tab[key] then
  print "key exists in the table"
else
  print "key does not exist in the table"
end

does not work any more, because tab[key] could exist, yet hold the value of
false. So you _have_ to write it like "if tab[key]~=nil then..."

With the current behavior, this convenience is non existant, so I fail to
see what is the reasoning behind 0 still being true? Since there are already
two different values of logical false, why not add two more and have 0 and
"" logically false as well?

For interfacing with C programs, and for general convenience of the
scripting user, we are considering patching the lua in our app, to have zero
and empty string mean the same thing (logically) as false as nil currently
do. Does anyone see why would that be a bad idea?

OK, I am late in my reading of the list, but I wanted to answer anyway, before reading the existing answers...
The habit of using 0 or NULL or '\0' as false in C is, IMHO, not very good.
Sure, the while (*p) or if (p && p->Foo()) idioms are short and some may find them expressives, but I definitively choose to allow (in my code) such form only for variables typed as boolean (and prefixed with the b letter: bEndReached or such).

So, even in Lua, I always test if x ~= nil then, in C if (*p == '\0') or if (p != NULL). It seems cleaner this way, consistent with strong typing (at least in C/C++/Java) or strong discipline about typing.

Just my 2€c.

--
Philippe Lhoste
--  (near) Paris -- France
--  http://Phi.Lho.free.fr
--  --  --  --  --  --  --  --  --  --  --  --  --  --