[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Unexpected behavior with "not not"
- From: Wim Couwenberg <w.couwenberg@...>
- Date: Thu, 30 Jun 2005 19:47:37 +0200
> 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
function.)
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... :-) )
--
Wim