|
|
||
|
Am 24.03.2018 um 07:06 schröbte Sergey Zakharchenko:
Philipp,
Hi!
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.Not sure we should introduce a separate metamethod for that, would be nice to check memory usage/processing overhead in these cases. BTW while we're discussing all of this, pairs() seems to pass only 1 argument to __pairs, but I didn't suggest changing *that*.
Not sure I can follow the second part. I thought about something like
do
local function firstonly( f, ... )
return function( s, var )
return (f( s, var ))
end, ...
end
function keys( obj )
local mt = getmetatable( obj )
if type( mt ) == "table" and
type( mt.__keys ) == "function" then
return mt.__keys( obj )
else
return firstonly( pairs( obj ) )
end
end
end
which reuses the `pairs` protocol as is but only returns the keys.
I agree on the first part though. It seems that in other languages you
would look for some method to call while in Lua we use a metamethod
instead if we want customized behavior.
Best regards,
Philipp