lua-users home
lua-l archive

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


Following are further notes on this.

The list implementation uses nil as an end sentinel.  This value works
out nicely since it is the value of a nonexistent table key, and hence
the end sentinel need not be explicitly managed.  For example, when
appending to list { 'a', 'b' }, we need only set index 3 to the new
value, rather than having to both set the value to index 3 and set the
sentinel to index 4.

The issue is that some people (at least me) feel that nil is too useful
a value within lists to be relegated to the job of sentinel.  Lists are
commonly used to capture function input and output value sequences,
deterring use of nil in those cases as well.

At the very least, this being Lua, there should be a straightforward way
for users to implement support for nil values in lists, if that is
desired.  However, the list constructor syntax (e.g. {2, nil, 'bar',
nil}) drops the sequence length information from the start, so support
there would at least require a core change.  A less attractive approach
is to give up the native constructor syntax, and use something like
Array(2, nil, 'bar', nil), since an accurate vararg length is available
via select.  However, besides being less efficient, a plain table could
not be used in the implementation, since that type does not support the
len metamethod.  Using of another type (e.g. userdata, function closure)
would necessarily require C coding.

Prior to Lua version 5.0, nil values in a list were supported via the
"n" field, an optional length field honored by length and iteration
operations, and set by the core in the case of a vararg list.