[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [PATCH]: Lua Suggestion - assign functions *or* tables to __pairs/__pairs
- From: Dirk Laurie <dirk.laurie@...>
- Date: Sat, 21 Dec 2013 22:53:45 +0200
2013/12/21 Sir Pogsalot <sir.pogsalot@gmail.com>:
> My patch does not add that behavior -- the patch is only about allowing
> table assignment to __pairs/__ipairs. Currently you can only assign a
> function.
>
> You should take a look at this: http://www.lua.org/pil/13.4.4.html
>
> Using __pairs / __ipairs allows you to make "proxy tables" practically
> invisible to the user because with it you can iterate over the proxy as
> though you were iterating over the table it references. Proxy tables are
> especially useful for implementing the observer pattern without explicitly
> emitting signals to trigger callback functions. Very cool stuff :-)
Very confusing stuff. I didn't understand it the first time round.
The confusing bit is that the patch is not about allowing table assignment
to __pairs/__ipairs. That can already be done. You'll get an error only
later.
It is about changing the semantics of the pairs/ipairs builtins so that if
a table instead of a function is found, iteration is done over that table.
I don't see why one needs a patch for that. It is easy enough in plain
Lua.
pairs = function (tbl)
local mt=getmetatable(tbl)
if mt then
local pairs=mt.__pairs
if pairs then
if type(pairs)=='function' then return pairs(tbl)
elseif type(pairs)=='table' then return next,pairs
end
end
end
return next,tbl
end