lua-users home
lua-l archive

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


On Tue, Jul 2, 2013 at 7:08 PM, Luis Carvalho <lexcarvalho@gmail.com> wrote:
> Coda Highland wrote:
>> On Tue, Jul 2, 2013 at 5:34 PM, Luis Carvalho <lexcarvalho@gmail.com> wrote:
>> >> Is something like this enough? (Note that 'n' is *not* hidden; I do
>> >> not think hidden stuff is good in the end...)
>> >
>> > Agreed. I had a very similar version, but required that only integer keys can
>> > be set in __newindex and setlength is implemented through __call:
>>
>> --snip--
>>
>> You didn't put in a way to shrink the array. Probably sufficient to
>> handle calling newindex on n, I suppose.
>
> No, since 'n' is an existent field (so newindex won't be called.) You can grow
> the array by setting a[k] for k > t.n, but to really shrink -- that is, to set
> t[k] = nil for k > n -- you have to use __call: a(n). That's just a matter of
> taste, and it might be better to have an explicit setlength as in Roberto's
> implementation, but I thought that shrinking the array would be rare and not
> worth of a dedicated method (and setting __index.)
>
> Cheers,
> Luis
>
> --
> Computers are useless. They can only give you answers.
>                 -- Pablo Picasso
>
> --
> Luis Carvalho (Kozure)
> lua -e 'print((("lexcarvalho@NO.gmail.SPAM.com"):gsub("(%u+%.)","")))'
>

Being able to shrink an array is vital to using it as a stack. If you
don't need to push nils, you can use "t[#t] = val" for a push
operation and "t[#t] = nil" for a pop operation. If you have auto-grow
functionality, then "t[#t] = val" still works, but you'd need
something like "t.resize(#t-1)" for pop.

/s/ Adam