lua-users home
lua-l archive

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

On Sat, Mar 24, 2018 at 9:01 AM, Dirk Laurie wrote:
2018-03-24 7:08 GMT+02:00 Egor Skriptunoff:
> On Sat, Mar 24, 2018 at 3:24 AM, Philipp Janda  wrote:
>> Am 23.03.2018 um 09:41 schröbte Sergey Zakharchenko:
>>> Dirk,
>>>>> 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".
>>> Sure I can. However, certain kinds of (heavily meta) tables may have a
>>> high cost of value retrieval, which this proposal aims to eliminate.
>> When using the `pairs` function, I expect getting pairs. An iterator factory for keys only would better be called `keys()`. Implementation-wise it could look for a `__keys` metamethod for those performance critical cases and fall back to the `pairs` protocol but dropping the second value.
> for k in pairs(tbl, 'k') do
> for v in pairs(tbl, 'v') do
> for k, v in pairs(tbl, 'kv') do  -- the default value of second argument

Neat, especially the second one — but the idiom 'for _,v in pairs(tbl)
do' is so well entrenched .

ipairs is actually not needed if we could use
for k,v in pairs(tbl, 'ikv') do  -- Integer Keys and Values
instead of
for k,v in ipairs(tbl) do

for v in pairs(tbl, 'iv') do  -- Integer Values
is the equivalent of
for _,v in ipairs(tbl) do