lua-users home
lua-l archive

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


> On Jun 27, 2016, at 11:07 PM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
> 
> The thread has been hijacked, but this OP enjoys the direction
> in which it is running.
> 
> 2016-06-28 5:29 GMT+02:00 Tim Hill <drtimhill@gmail.com>:
>> 
>> To be honest I’ve never liked the use of “n” as a pseudo-length.
>> First, it’s inconsistent (collides with use of #). Second, it’s hi-jacking
>> a perfectly normal user key with very little notice (why wasn’t a reserved
>> “_N” used?).
> 
> Why must such a key match "[_%a]%w*"? Can't we reserve tbl["#"]
> for the purpose? With the semantics of "__len" extended to treat
> that as a first fallback? I.e.

I was just going by the Lua ref manual that states that leading underscore + upper case are reserved for Lua use, hence “_N”. “#” is interesting, but technically it’s a valid user key so could collide with other things (I’m sure someone has built a token parser where the tokens are table keys).

> 
> 1. If the __index metamethod exists, use it.
> 2. If not, use tbl["#"] as the length.
> 3. If that is nil or false, revert to the built-in algorithm.
> 
>> My personal feeling is that we need a first-class explicitly set length.
> 
> ["#"] is less convenient that .n or ._N, sure, but do we want it to
> be convenient? When debugging, something as drastic as
> redefining the length of a table should be obtrusively visible.
> 

As others have noted, "#t = (some expression)” might work, but there are probably corner cases here in Lua syntax (like the infamous parens around a function call).

—Tim