lua-users home
lua-l archive

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


2018-03-14 8:04 GMT+02:00 Russell Haley <russ.haley@gmail.com>:
>> for i,v in pairs(u) do print(i,v) end
> 1       one
> 2       two
> 3       four
> 4       nil
> 5       five
>> #t
> 5
>> t[4] = undef
>> for i,v in pairs(t) do print(i,v) end
> 1       one
> 2       two
> 3       four
> 5       five
>> #t
> 3  --<<<<<< I Expected 4 here?

The above results were verified with 'lua' built on Ubuntu 16.04 LTS
from inside the src directory by

   make -e MYFLAGS=-DLUA_NILINTABLE linux-readline

The feature seems to implemented in the following way:

1. type 'nil' now has a subtype LUA_TEMPTY.
2. There are two values of type 'nil', one of each subtype, with
corresponding predefined objects nil and empty (my name; it is
actually 'luaH_emptyobject'). However, if LUA_NILINTABLE is not set,
these objects are the same.
3. t[i] = nil stores empty, which is just nil if LUA_NILINTABLE is not set.
4. t[i] = undef stores nil regardless.
5. A macro isempty() is defined which distinguishes (or not, depending
on LUA_NILINTABLE) between nil and empty.
6. The difference seems not to be directly visible in the C API (no
lua_isempty).
7. Most (but not all) cases, especially in the support for tables,
where previously there was a test for a nil value now use isempty()
instead. In particular, the function which searches for a boundary
(i.e. the default for '#') does so.
8. The table library is written in the API, so cannot test for empty.
It is affected indirectly because it uses #.

We can now argue if we like whether it would not have been less
confusing there was no 'undef', and 'empty' was a second predefined
value of tye 'nil', with t[i] = nil and t[i] = empty meaning what they
seem to — but that would break nearly all existing programs in Lua.