[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Metatables are "hard" (Re: Question about __index and classes)
- From: Mark Hamburg <mark@...>
- Date: Mon, 29 Aug 2011 07:28:05 -0700
On Aug 28, 2011, at 11:18 PM, Dirk Laurie wrote:
> On Mon, Aug 29, 2011 at 06:54:48AM +0200, Josh Simmons wrote:
>> __newindex is an odd wording though I'm not sure it'd be really worth
>> changing at this point.
> On the contrary, it is a perfect name.
> It is the name of a routine that is only invoked if you assign
> something to tbl[idx] when idx is not an existing index into the
> table; that is to say, when idx is a new index.
> Since most of the confusion arises because folk think it is invoked
> for *every* assigment into a table, it is a much more suitable name
> than e.g. __assign or __setindex would have been.
Which is probably because people are looking for the latter and try to pretend that __newindex satisfies that only to be sorely disappointed.
An override-based language would almost certainly provide assign because that's the source-level operation. In Lua's case, the fallback only gets used when the runtime can't just update an existing table entry and, while perfectly clear when you understand this, that takes more thought to figure out how to use effectively when building software (particularly if you were looking to catch all assignments).