lua-users home
lua-l archive

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


On 2/15/11, Matthew Frazier <leafstormrush@gmail.com> wrote:
> (...)
> So, I don't think getting rid of nil is the answer. I think the answer
> lies in a stronger way of defining which values are actually defined,
> beyond "does it equal nil." I'm borrowing from Python here, but "in" and
> "del" keywords for testing whether a key is actually *defined* in a
> table (i.e. "3 in t") and deleting keys from a table (i.e. "del t[3]")
> might be useful for this. This obviously isn't a complete solution to
> the problem, but it would definitely sort out a lot of the confusion.
>
> --
> Regards, Matthew Frazier
>
>

t.key == nil is the test and t.key = nil is the delete operation. nil
is not a first-class value nor is it meant to be: you can't store nils
in tables and that's a feature, not a bug.

The case where you use an uninitialized key in a table can be solved
with a simple:

setmetatable(t,{__index = function(t,k) error('key is absent') end})

Unintended deletions are harder, but it's part of the contract that
the 'client' checks if the value (or the key) is nil. Frankly I'd
prefer if t[nil] was a noop, for consistency.