lua-users home
lua-l archive

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

i was initially unsure about lua's use of 0==true, but now i use it a lot :

conditional formulae :
a = b or a -- would fail if b==0 and 0==false
a = a + (b or default_inc) -- ditto

will fail if 0 == false, as will many other similar constructs i use.

tables with flags :

t[v] = nil : flag not defined
t[v] = false : flag defined but not set
t[v] = true : flag defined and set.

think also re metetables :
t[v] = false
if (t[v]) then --no mt __index lookup

t[v] = nil
if(t[v]) then -- metatable __index is called

function returns :

local ret, err
repeat ret, err = f() -- return false to break, nil, err on error
until not ret

assert(ret ~= nil,err) -- false return is not an error

ps i did some testing a while back :
if t[v] then ....
appears to be faster if t[v] == false than if t[v] == nil, probably because of the metatable lookup invoked for nil.

in short - i like it the way it is
nil and false are not synonymous; anything else is true.