lua-users home
lua-l archive

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


On Sat, Mar 17, 2018 at 11:55 AM, Stephan Hennig <sh-list@posteo.net> wrote:
> Am 17.03.2018 um 17:06 schrieb Stephan Hennig:
>
>> I tend to think tables with nil are constant size or grow only.  So
>> here's a stupid question:
>>
>> What use-case is there to have nil in tables /and/ to be able to delete
>> nil from tables?
>
> Mixed two thoughts into one: What use-case is there to have nil in
> tables /and/ to be able to delete (any) values from the same tables?
>
> Best regards,
> Stephan Hennig

One realistic example: Imagine a cache table that holds the results of
expensive function calls. The function can return nil, so you have to
be able to store that, and you need to be able to remove entries in it
as they become stale.

Without the ability to delete keys distinctly from storing nil, you
would have to use a singleton to act as a fake nil and then detect
that and replace it with a real nil at runtime. Possible, sure, but a
bit ugly.

Another realistic example: For performance reasons, you're trying to
reuse tables as much as you can. If you need to be able to distinguish
between a nil value and an absent key, then you HAVE to be able to
properly delete keys if you're going to reuse tables.

/s/ Adam