lua-users home
lua-l archive

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


On 06/07/2018 08:34 AM, Egor Skriptunoff wrote:
> I think Rodrigo is correct.
> We don't really need NIL_IN_DICTIONARY, we only need NIL_IN_ARRAY.
> And the "array border" (or "high water mark") is the right word to solve
> the problem.
> 
> Here is one more (very similar) suggestion for 5.4 which don't require new
> syntax.
> Every Lua table must store 8-byte integer "high water mark" (biggest
> non-zero positive integer key assigned).
> User can read it with function table.hwm(t)
> 
> [...]
> 
> As you see, the logic of hwm(t) is completely independent of #t.
> We have both traditional length #t and array-ish length hwm(t)
> simultaneously,
> that solves our problem with nils in arrays.
> 
> How arrays with nils should be traversed in 5.4:
> for k = 1, table.hwm(array) do ... end
>
> [...]
>
> -- Egor

Wow, I like this proposal!

I imagine this as a __newindex hook that sets hidden "hwm" field to a maximum
of current "hwm" and integer key value. Small overhead, almost free.

> Questions:
> 1) Is table.sethwm(t, newhwm) needed?
Yes. Stored such way water mark should not be immutable.

> 2) Should ipairs() use table.hwm() internally?  Is new function hpairs()
> needed?
I think no. Or it'll break existing Lua code. Introduce "hpairs()"

> 3) Is __hwm metamethod needed?
Probably yes. What parameters it'll have?

-- Martin