lua-users home
lua-l archive

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


On Wed, Oct 19, 2011 at 8:12 PM, Daurnimator <quae@daurnimator.com> wrote:
> A small feature request to distract you all from the noise on the list recently:
>
> I often find myself writing code like
> mt.__newindex = function ( t , k , v )
>    v = dosomethingwithv ( v )
>    rawset(t,k,v)
> end
>
> Could we make it so the return value of __newindex is then set as the
> value in t?

Consider proxy tables. You never actually add anything to the proxy
table. All indexes and sets are redirected. e.g.

t = {}
proxy = {}
setmetatable(proxy, {__index = t, __newindex = function(_,k,v) t[k] = v end})

[I think you can actually just set __newindex to t, but this should
work regardless.]

-- 
- Patrick Donnelly