[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: __newindex rawsetless semantics?
- From: "Thiago L." <fakedme@...>
- Date: Tue, 14 Oct 2014 11:18:59 -0300
On 14/10/14 11:08 AM, Javier Guerra Giraldez wrote:
On Tue, Oct 14, 2014 at 8:22 AM, Thiago L. <fakedme@gmail.com> wrote:
On 14/10/14 10:18 AM, Javier Guerra Giraldez wrote:
On Tue, Oct 14, 2014 at 8:06 AM, Thiago L. <fakedme@gmail.com> 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.