lua-users home
lua-l archive

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


2018-03-23 10:05 GMT+02:00 Sergey Zakharchenko <doublef.mobile@gmail.com>:
>
> Hello list,
>
> I have some half-baked ideas on conservative but potentially useful
> adjustments to metamethod protocols. No Lua implementation code
> changes required (just yet). It's all about convention. "We cannot
> wait for Lua creators' favour, our aim is to help ourselves" (slightly
> evil grin).
>
> * Keys-only __pairs semantics.
>
> If __pairs is invoked wth a second argument equal to the special value
> false, the caller is not interested in values (only in keys), so the
> returned iterator may returned any value, including nil, or no value
> at all, for them.

You can already write "for k in pairs(tbl,false) do ... end".

> * Key-adjusting __newindex semantics.
>
> If __newindex is invoked with a special fourth argument (what kind of?
> equal to false?), then the metamethod is expected (but not required)
> to perform insertion (a la table.insert) if the vale is non-nil, or
> removal (a la table.remove) if the value is nil. This may or may not
> include:
> - adjusting indices of other values;
> - raising an error if adjustment is not possible (e.g. the key is a
> string and already used) or there is no such element to remove;
> - supporting nil as key;
> - returning the (actual) key used.

I can't figure out what the idea is. Give an example.