Feature Request: rawset return value from __newindex

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Feature Request: rawset return value from __newindex

Daurnimator
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?

It will be backwards compatible, as the majority of code out there
don't return anything from __newindex operations.

Benefits
 - One less line of code
 - __newindex operations can now do a tail call; `function (t,k,v)
return dosomethingwithv(v) end;
 -  I assume it can be optimised quite well in the actual
implementation, as the key has already been hashed; and we know its
not already in the table.


Daurn.

Reply | Threaded
Open this post in threaded view
|

Re: Feature Request: rawset return value from __newindex

Patrick Donnelly
On Wed, Oct 19, 2011 at 8:12 PM, Daurnimator <[hidden email]> 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

Reply | Threaded
Open this post in threaded view
|

Re: Feature Request: rawset return value from __newindex

Daurnimator
On 20 October 2011 11:16, Patrick Donnelly <[hidden email]> wrote:

> 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
>
>


In that case, your example still works: not returning anything results
in returning a nil; this will then set a nil in a table: which does
nothing

ALso, you can't set __newindex to a table: that only works with
__index (Though it would be a nice improvemet if there is no
performance loss)

Reply | Threaded
Open this post in threaded view
|

Re: Feature Request: rawset return value from __newindex

Mike Nelson
On 10/19/2011 6:40 PM, Daurnimator wrote:

> On 20 October 2011 11:16, Patrick Donnelly<[hidden email]>  wrote:
>> 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
>>
>>
>
> In that case, your example still works: not returning anything results
> in returning a nil; this will then set a nil in a table: which does
> nothing
>
> ALso, you can't set __newindex to a table: that only works with
> __index (Though it would be a nice improvemet if there is no
> performance loss)
>
>

Per Lua 5.2 Reference Manual, section 2.4, __newindex can be set to a
table. (Same as Lua 5.1)

-- Mike Nelson

Reply | Threaded
Open this post in threaded view
|

Re: Feature Request: rawset return value from __newindex

Peng Zhicheng
In reply to this post by Daurnimator
于 2011年10月20日 08:12, Daurnimator 写道:

> 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?
>
> It will be backwards compatible, as the majority of code out there
> don't return anything from __newindex operations.
>
> Benefits
>   - One less line of code
>   - __newindex operations can now do a tail call; `function (t,k,v)
> return dosomethingwithv(v) end;
>   -  I assume it can be optimised quite well in the actual
> implementation, as the key has already been hashed; and we know its
> not already in the table.
>
>
> Daurn.
>
the __newindex meta method, or event, is triggered when a non-existing field of one table is being set.
so the semantic type is an assignment *statement*, not an *expression*. so it is meaningless to return values.

so the __index event is to evaluate an expression, while __newindex is to execute a statement.