lua-users home
lua-l archive

not

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


Maybe there is a trick here I don't see, but.....
Many times in my Lua code, I'll test that a key appears in a table and do
something:
 if t.whatever then
 do_something()
 end
But today was the first time that I found I needed to test for the opposite
case-- that of t.whatever not existing. It's then I realized that Lua
doesn't have a logical "not".
It's easy enough to create a function that implements a logical "not":
 function not(x)
 if x then
 return nil
 else
 return 1
 end
 end
And use it like this:
 if not(t.whatever) then
 do_something()
 end
But is forcing a functional syntax and having the cost of of a function
call what the designers of Lua intended? I admire the minimalism in Lua,
but this seems like a pretty basic need for most any language. Without it,
you're either forcing the programmer to come up with their own logical
"not" or forcing them to have an empty "then" clause in an "if," doing the
work in the "else" clause.

AltStyle によって変換されたページ (->オリジナル) /