lua-users home
lua-l archive

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


Philippe Lhoste wrote:
> > Note that false is a valid table value, and in that case the test 
> > above will fail. To be sure a key is not used check 
> explicitly against nil.
> 
> Yes, that's a bad habit from C (or older Lua versions...).
> I always test explicitly against nil, even when there is no 
> ambiguity, and use the if foo then only with variables I know 
> being booleans.
> Idem in other languages, whatever their tolerance for this idiom.
> Just a personal policy, of course.

In fact some considers that to be a *good* habit in C++, where you can
overload the cast-to-bool operator. This makes a good universal paradigm
to use: if something resolves to true it's valid (and you can use it).
It works very well with Resource Acquisition Is Initialization
technique. This is true for basic pointers, and you can easily make it
true for objects (smart pointers, file handlers, etc).