lua-users home
lua-l archive

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


I would say that from a CS perspective, table.has() and an "empty" sentinel are the same thing in different form; both introduce an extra bit of information about an element in the table, since you now have two possibilities when faced with t[k] = nil: one with table.has() true, one with table.has() false. Logically you could define:

function empty(t, k) return t[k] == nil and table.has(t,k) end

--Tim

On Jul 1, 2013, at 12:13 PM, Coda Highland <chighland@gmail.com> wrote:

> On Mon, Jul 1, 2013 at 12:11 PM, Tim Hill <drtimhill@gmail.com> wrote:
>>>> That could be useful, but also a bit confusing as it means a key set to nil
>>>> and a key that was never set (or was deleted) are different things, but both
>>>> have a nil value.
>>> You can tell the difference with table.has().
>> Depending on how # behaved, you might also be able to tell with just (i <=
>> #t) ?
> 
> table.has() is a generic mechanism for all tables, not just arrays --
> a sparse array or a freeform table could still have nil-valued entries
> as distinct from nonexistent entries.
> 
> /s/ Adam
>