[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: new "empty" value/type in Lua?
- From: Coda Highland <chighland@...>
- Date: Tue, 2 Jul 2013 19:19:28 -0700
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
- References:
- Re: new "empty" value/type in Lua?, Thomas Jericke
- Re: new "empty" value/type in Lua?, Eike Decker
- Re: new "empty" value/type in Lua?, Roberto Ierusalimschy
- Re: new "empty" value/type in Lua?, Hisham
- Re: new "empty" value/type in Lua?, Roberto Ierusalimschy
- Re: new "empty" value/type in Lua?, Thomas Jericke
- Re: new "empty" value/type in Lua?, Roberto Ierusalimschy
- Re: new "empty" value/type in Lua?, Luis Carvalho
- Re: new "empty" value/type in Lua?, Coda Highland
- Re: new "empty" value/type in Lua?, Luis Carvalho