lua-users home
lua-l archive

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

> print(not not nil)              --> false
> print(not not nil and true)     --> false
> print(not not (nil and true))   --> nil (!)
> print(not (not (nil and true))) --> nil (!)

That's funny!  The reason seems to lie in some tiny optimisations in
lcode.c (that's in 5.0.2):

The compiler makes the following substitutions if nil, false or true
are encountered as *constants*:

        not nil --> true
        not false --> true
        not true --> false

so "not not nil" is directly emitted as "false".  (See the codenot

On the other hand, "not not <expr>" is replaced by "<expr>", so "not
not (nil and true)" is replaced by "nil and true" which evaluates to
nil.  (See the jumponcond function.)

I'd guess that this was an oversight when introducing true and false
in Lua 5?

(I hope this explanation is at least somewhat accurate...  :-)  )