lua-users home
lua-l archive

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


Consider adding a new value next to "nil": "undefined".

local t = {}
print ( t["somestring"])  --- prints "undefined"
t["somestring"] = nil
print ( t["somestring"])  --- prints "nil"

Both undefined and nil evaluates to false when used in an if statement.

For loops will not stop until it encounters an "undefined" value.
Same for the # length operator.

For pairs will show all key-value pairs where the value is not undefined.

Keeping it simple and making it easier to grasp, at the same time, without being pedantic.

On 16/02/2011, at 7:50 PM, Luiz Henrique de Figueiredo wrote:

But nil isn't really first-class, because it cannot really be stored
as value into tables.

You can definitely store nil in a table in the sense that t[1]=nil gives
you back nil when you read t[1].

"storing" it in a table makes the key go away

This reflects our view of tables, which are containers for values.
Keys play a secondary role. When there is no value, there's no key.
This behavior is *not* due to some flaw in nil.

not show up on i/pairs

One way to think about a table is that it initially contains *all* pairs (key,nil), for all possible keys. You wouldn't want pairs to show them all :-)