lua-users home
lua-l archive

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


On Fri, Aug 1, 2014 at 1:04 PM, Sean Conner <sean@conman.org> wrote:
> It was thus said that the Great Coda Highland once stated:
>> On Fri, Aug 1, 2014 at 11:04 AM, Coroutines <coroutines@gmail.com> wrote:
>> > On Fri, Aug 1, 2014 at 11:00 AM, Roberto Ierusalimschy
>> > <roberto@inf.puc-rio.br> wrote:
>> >>> It was mentioned that "table.clear" (or similar) was likely. Was it decided
>> >>> that it would not be useful?
>> >>
>> >> I don't think we even agreed on what it would do.
>> >
>> > As far as I see it, tables and closures are the only real mutable base
>> > types in Lua -- we generally don't need to nil'ify all the upvalues of
>> > a closure/function, but it would be nice to quickly dispense the array
>> > and hash portion of a table so the same object can be returned, and
>> > filled thereafter -- with the same associated metatable.  Just
>> > something faster than looping through the table in Lua to unreference
>> > everything?  I'm not sure what extra arguments it might take to
>> > selectively clear portions of the table.  Maybe the array or hash
>> > portion, or a starting and ending numeric index?  I might be very
>> > crazy... :p  Stop me if I have weird ideas.
>> >
>>
>> Oh STAHP! ;)
>>
>> Seriously, though, I can't see any real uses for it beyond just a
>> complete and total table wipe. Anything else is either adequately
>> served by nil assignment, or is an implementation detail that isn't
>> exposed anyway.
>
>         table.clear(x)
>
> vs
>
>         do
>           local y = getmetatable(x)
>           x = {}
>           setmetatable(x,y)
>         end
>
>   -spc (Hmmm ... now that I think about it, slap that code into a function
>         and there you go ... )

That doesn't work.

function tableclear(x)
  local y = getmetatable(x)
  x = {}
  setmetatable(x,y)
end

foo = { a=1 }
tableclear(foo)
print(foo.a) -- outputs "1"

Regardless, I was referring to wiping the data in the table, not the
metatable. I suppose an argument could be made that it should be
table.clear(table[, removeMetatable]) to support both cases, but I
can't see any sense in clearing ranges.

/s/ Adam