[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Possible bug with the length operator
- From: HyperHacker <hyperhacker@...>
- Date: Fri, 1 Apr 2011 09:28:57 -0600
On Fri, Apr 1, 2011 at 08:58, Julien Duminil <julien.duminil@ubisoft.com> wrote:
> I agree, #t is enough useful, IMHO, to let it as it is (until someone found a better solution, of course).
> Moreover, as said in http://www.lua.org/work/doc/, "tables honor the __len metamethod" in lua 5.2.
>
> But I was wondering if __len and __newindex are enough to easily emulate all four of these options, as you can't detect adding some "holes" in your table.
> So here is my question: would it be interesting to add a __delindex or __remindex metamethod called when an existing table index/key is setted to nil?
>
> Not sure it is a really useful/interesting feature (definitively not a request from me, i'm just sharing the idea)...
>
> PS: __del/remindex could also help to prevent removing some keys from a table (some kind of readonly?).
>
> >From HyperHacker:
>>
>>Or just let #t remain as-is, but have it honour the __len metamethod
>>for tables, so that one is free to define it however they like for
>>each table. Make it clear in the manual that if your array has holes
>>and you don't define __len, then #t has no real meaning. Between
>>__len and __newindex, one can emulate all four of these options.
>
>
>
>
__newindex is called even if the value being set is nil. The problem
is that __newindex isn't called if the key already exists (presumably
for optimization). So you need to use __index and __newindex to proxy
all accesses to a hidden table somewhere so that you can catch those
nil assignments. Then you're free to handle "holes" however you like.
--
Sent from my toaster.