lua-users home
lua-l archive

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


> On Nov 19, 2013, at 5:40 PM, Paige DePol <lual@serfnet.org> wrote:
> 
> Well right now 0 is "mostly false" as Douglas Adams may say... lol! Boolean operations using 0 do treat it as false, including and/or, just those equality operators that are being tricksy when using the boolean true/false keywords! ;(
> 
> I am interested in tracking down why the equality operators do not treat 0 as false though, mostly for my own understanding, but also so I can have a true "0 is always false" patch with no half-measures! ;)
> 
> ~pmd~

Here I go replying to myself again, however, it is worth it! :)

I figured out where the issue with the equality operators was and it is a pretty simple fix. Essentially I needed to add an extra equality check that is run if two objects being compared are not the same type. If not the new routine just checks if one is a bool and one is a number and returns "(number > 0) == bool".

All tests now work, 0 now equals false as expected in all assignment and boolean operations. Truly, zero can now be false in Lua, though it is not really recommended that you patch Lua, it is more a patch for variants of Lua! ;)

Please find attached the patch file for the Zero as False patch. The patch can be enabled or disabled in luaconf.h, look for the LUA_EXT_ZERO_IS_FALSE define.

~pmd~


Attachment: zeroisfalse.patch
Description: Binary data