__newindex rawsetless semantics?

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

__newindex rawsetless semantics?

Soni "They/Them" L.
Hi,

I guess you can claim this is a proposal but I also want feedback on it...

The idea is new semantics for __newindex so you can use it without rawset():

function __newindex(t,k,v)
   if condition then
     return k,nv
   elseif othercondition then
     return nk,v
   elseif anothercondition then
     return nk,nv
   else
     return k,v
   end
end

Unless your __newindex returns junk, this shouldn't break anything.

Thanks.

Reply | Threaded
Open this post in threaded view
|

Re: __newindex rawsetless semantics?

Karel Tuma-2
Excerpts from Thiago L.'s message of 2014-10-14 13:42:26 +0200:
> Unless your __newindex returns junk, this shouldn't break anything.

Some transpilers (moonscript...) have implicit return. Junk happens.

Reply | Threaded
Open this post in threaded view
|

Re: __newindex rawsetless semantics?

Soni "They/Them" L.

On 14/10/14 09:11 AM, Karel Tuma wrote:
> Excerpts from Thiago L.'s message of 2014-10-14 13:42:26 +0200:
>> Unless your __newindex returns junk, this shouldn't break anything.
> Some transpilers (moonscript...) have implicit return. Junk happens.
>
Does "implicit return" mean "return nil"? because if so that shouldn't
be a problem...

Reply | Threaded
Open this post in threaded view
|

Re: __newindex rawsetless semantics?

Roberto Ierusalimschy
In reply to this post by Soni "They/Them" L.
> The idea is new semantics for __newindex so you can use it without rawset():
>
> function __newindex(t,k,v)
>   if condition then
>     return k,nv
>   elseif othercondition then
>     return nk,v
>   elseif anothercondition then
>     return nk,nv
>   else
>     return k,v
>   end
> end

Would that be exactly equivalent to this?

  function __newindex(t,k,v)
    if condition then
      rawset(t,k,nv)
    elseif othercondition then
      rawset(t,nk,v)
    elseif anothercondition then
      rawset(t,nk,nv)
    else
      rawset(t,k,v)
    end
  end

What is the gain?

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: __newindex rawsetless semantics?

Soni "They/Them" L.

On 14/10/14 10:03 AM, Roberto Ierusalimschy wrote:

>> The idea is new semantics for __newindex so you can use it without rawset():
>>
>> function __newindex(t,k,v)
>>    if condition then
>>      return k,nv
>>    elseif othercondition then
>>      return nk,v
>>    elseif anothercondition then
>>      return nk,nv
>>    else
>>      return k,v
>>    end
>> end
> Would that be exactly equivalent to this?
>
>    function __newindex(t,k,v)
>      if condition then
>        rawset(t,k,nv)
>      elseif othercondition then
>        rawset(t,nk,v)
>      elseif anothercondition then
>        rawset(t,nk,nv)
>      else
>        rawset(t,k,v)
>      end
>    end
>
> What is the gain?
>
> -- Roberto
>
the gain is that I don't have to expose rawset...

Reply | Threaded
Open this post in threaded view
|

Re: __newindex rawsetless semantics?

Javier Guerra Giraldez
On Tue, Oct 14, 2014 at 8:06 AM, Thiago L. <[hidden email]> wrote:
> the gain is that I don't have to expose rawset...


but what if i don't want to set anything to this table?

--
Javier

Reply | Threaded
Open this post in threaded view
|

Re: __newindex rawsetless semantics?

Soni "They/Them" L.

On 14/10/14 10:18 AM, Javier Guerra Giraldez wrote:
> On Tue, Oct 14, 2014 at 8:06 AM, Thiago L. <[hidden email]> wrote:
>> the gain is that I don't have to expose rawset...
>
> but what if i don't want to set anything to this table?
>
then you don't return anything? (or return nil key?)

Reply | Threaded
Open this post in threaded view
|

Re: __newindex rawsetless semantics?

Javier Guerra Giraldez
On Tue, Oct 14, 2014 at 8:22 AM, Thiago L. <[hidden email]> wrote:
> On 14/10/14 10:18 AM, Javier Guerra Giraldez wrote:
>> On Tue, Oct 14, 2014 at 8:06 AM, Thiago L. <[hidden email]> wrote:
>>> the gain is that I don't have to expose rawset...
>>
>> but what if i don't want to set anything to this table?
>>
> then you don't return anything? (or return nil key?)

or set more than one value? or to a different table?  in the end you
need the equivalent of rawset()


unfortunately, it seems that this is a case of "all is well, don't
change anything".

Just guessing: this stems from the idea that __newindex is used mostly
to ultimately set a single value to the same table, and makes it the
default.  but if you remove the previous way to do it (rawset), you
have to provide all the capabilities.  in many cases by some contrived
and non-obvious constructions.

why not to expose rawset()?  is it for security reasons?  it might be
easier to provide a slightly patched version that only skips user's
__newindex but still respects those defined by your environment.

--
Javier

Reply | Threaded
Open this post in threaded view
|

Re: __newindex rawsetless semantics?

Soni "They/Them" L.

On 14/10/14 11:08 AM, Javier Guerra Giraldez wrote:
> On Tue, Oct 14, 2014 at 8:22 AM, Thiago L. <[hidden email]> wrote:
>> On 14/10/14 10:18 AM, Javier Guerra Giraldez wrote:
>>> On Tue, Oct 14, 2014 at 8:06 AM, Thiago L. <[hidden email]> wrote:
>>>> the gain is that I don't have to expose rawset...
>>> but what if i don't want to set anything to this table?
>>>
>> then you don't return anything? (or return nil key?)
> or set more than one value? or to a different table?  in the end you
> need the equivalent of rawset()

function __newindex(t,k,v)
     if k == "special key" then
         for i,val in ipairs(split(k,"\n")) do
             t[whatever + i] = val
         end
     else
         return k,v
     end
end

With my proposal the above piece of code would work just like this one
with current Lua:

function __newindex(t,k,v)
     if k == "special key" then
         for i,val in ipairs(split(k,"\n")) do
             t[whatever + i] = val
         end
     else
         rawset(t,k,v)
     end
end

If you *need* rawset, then... well don't use this sandbox :P There are
ways to work around lack of rawset:

local skip = false

function mt1.__newindex(t,k,v)
     if k == "special" then
         skip = true
         t2[v[1]] = t2[v[2]]
     else
         return k,v
     end
end

function mt2.__newindex(t,k,v)
     if skip then
         return k,v
     else
         error("Attempt to write to t2")
     end
end

-- etc

It also avoids a function call, if that matters...

So no, in the end you do NOT need the equivalent of rawset. Plus, the
proposal was never about removing rawset anyway...

>
>
> unfortunately, it seems that this is a case of "all is well, don't
> change anything".
>
> Just guessing: this stems from the idea that __newindex is used mostly
> to ultimately set a single value to the same table, and makes it the
> default.  but if you remove the previous way to do it (rawset), you
> have to provide all the capabilities.  in many cases by some contrived
> and non-obvious constructions.
>
> why not to expose rawset()?  is it for security reasons?  it might be
> easier to provide a slightly patched version that only skips user's
> __newindex but still respects those defined by your environment.
>

Reply | Threaded
Open this post in threaded view
|

Re: __newindex rawsetless semantics?

Roberto Ierusalimschy
In reply to this post by Javier Guerra Giraldez
> why not to expose rawset()?  is it for security reasons?  it might be
> easier to provide a slightly patched version that only skips user's
> __newindex but still respects those defined by your environment.

In Lua, there are all sorts of ways to provide some functions to some
environments but not to others. (For instance, you can save a local copy
of rawset in a module before removing it from the global environment.)

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: __newindex rawsetless semantics?

Tim Hill
In reply to this post by Soni "They/Them" L.

On Oct 14, 2014, at 4:42 AM, Thiago L. <[hidden email]> wrote:

> Hi,
>
> I guess you can claim this is a proposal but I also want feedback on it...
>
> The idea is new semantics for __newindex so you can use it without rawset():
>
> function __newindex(t,k,v)
>  if condition then
>    return k,nv
>  elseif othercondition then
>    return nk,v
>  elseif anothercondition then
>    return nk,nv
>  else
>    return k,v
>  end
> end
>
> Unless your __newindex returns junk, this shouldn't break anything.
>
> Thanks.
>

I think the biggest argument against this is that it only applies to tables. What about userdata? Having the added complexity of tables be a special cases doesn’t seem to be warranted by the minor convenience.

—Tim


Reply | Threaded
Open this post in threaded view
|

Re: __newindex rawsetless semantics?

Soni "They/Them" L.

On 14/10/14 05:39 PM, Tim Hill wrote:

> On Oct 14, 2014, at 4:42 AM, Thiago L. <[hidden email]> wrote:
>
>> Hi,
>>
>> I guess you can claim this is a proposal but I also want feedback on it...
>>
>> The idea is new semantics for __newindex so you can use it without rawset():
>>
>> function __newindex(t,k,v)
>>   if condition then
>>     return k,nv
>>   elseif othercondition then
>>     return nk,v
>>   elseif anothercondition then
>>     return nk,nv
>>   else
>>     return k,v
>>   end
>> end
>>
>> Unless your __newindex returns junk, this shouldn't break anything.
>>
>> Thanks.
>>
> I think the biggest argument against this is that it only applies to tables. What about userdata? Having the added complexity of tables be a special cases doesn’t seem to be warranted by the minor convenience.
>
> —Tim
>
>
For userdata it would end up in an attempt to rawset(ud,k,v) :P

Reply | Threaded
Open this post in threaded view
|

Re: __newindex rawsetless semantics?

Steven Degutis
Quoth SoniEx2:
> For userdata it would end up in an attempt to rawset(ud,k,v) :P

Then don't do it if type(t) isn't a table.