Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua not not the same as not not

Tags:

lua

I was using some coroutines and was trying to resume one using not:

if not co == nil then `resume end

and the coroutine wouldn't resume. Though co was nil. It was baffling.

So I eventually tried

if co then `resume end

and it worked!

why isn't not (nil == nil), which is logically false when co is nil and true when it is not which is not the same as as nil which is logically false when co is nil and true otherwise?

I use not all the time to negate the logical expression but now I'm worried that maybe some of my code is buggy. Maybe this only has a problem with coroutines? (and I'm 100% sure that the value is nil, because that is what is printed out, unless nil no longer equals nil)

like image 944
AbstractDissonance Avatar asked Oct 15 '25 03:10

AbstractDissonance


1 Answers

not co == nil is equivalent to (not co) == nil because of operator precedence.

If co is nil, then not co is true and so different from nil.

In fact, the result of not is always true or false, and so never equal to nil.

Write not (co == nil) or co ~= nil.

like image 159
lhf Avatar answered Oct 17 '25 03:10

lhf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!