[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Quest: real world "Lua array with holes" usage
- From: Dirk Laurie <dirk.laurie@...>
- Date: Tue, 26 Jul 2016 10:35:45 +0200
2016-07-26 0:19 GMT+02:00 Hisham <h@hisham.hm>:
> (1) As a preliminary, needed for the stuff below,
Forget about the stuff below. The idea swims better without all that.
> # becomes a special valid table key (note that # ~= "#").
Having proposed "#" for this purpose not too long ago, and having
been shot down because it is no less preemptive than "n", I am
delighted that you have found a better way.
> * #t is redefined to return t[#] if this key is set, or the Lua 5.3
> semantics of #t if it is not.
> * pairs(t) always skips t[#]
> * if t[#] is set, ipairs(t) traverses up to t[#]
The actual implementation of t[#] would be a field in Table,
so the next two properties are free.
This does raise the question whether the t[#] syntax is so very
intuitive. Would it not be better to resuscitate getn/setn as
getlen/setlen which would get or set not a table field, but that
otherwise inaccessible field?
2016-07-26 9:30 GMT+02:00 Thomas Jericke <tjericke@indel.ch>:
> First try
> { x, y, z, # }
> is syntactic sugar for
>
> { [1] = x, [2] = y, [3] = z, [#] = # }
>
> A simply # in the constructor sets t[#] at the current length (t[#] = #t). I
> am not sure about it though, as # acts here as an operator and not
> as a value.
I like the notation { x, y, z, # }. You could even have { x, y, z, #, a, b }
when a and b would be considered to be outside the sequence.
But as before, I don't like the explanation of # as syntactic sugar,
since it involves needing to think of (#,t[#]) as a key-value pair.
The explanation that I prefer is that # when inside a table literal is like
the empty capture in a string pattern, picking up position, and when
inside a table index means the length (even when __len is provided
or when no explicit length has been set). E.g. I want t[#-1] to mean t[#t-1].