lua-users home
lua-l archive

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


On Sat, Dec 21, 2013 at 5:00 PM, Dirk Laurie <dirk.laurie@gmail.com> wrote:

Because it is not unreasonable to expect that when one
does `for k,v in pairs(a) do`, `v` will be a value from `a`.
The metamethod might supply some cases where `v` is
nil, it might ignore some key-value pairs, but if you deny
me the comfort of the assertion `a[k]==v`, I will be lost.
 
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 :-)