lua-users home
lua-l archive

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




On 2018-03-17 02:40 PM, Egor Skriptunoff wrote:
On Sat, Mar 17, 2018 at 7:39 PM, Jay Carlson wrote:

    > New syntax {...#} is used for constructing a table
    > with its "user-specified length" set to actual data length.
    >
    > Probably, syntax {...#} is not nice.

    I wonder how much code would break by replacing the existing
    meaning of #{}.
    ...
    For completeness, add the sugar:
      f#{math.pi} <=> f(#{math.pi})


The syntax I suggest is {...#},  not #{...}
#{...} should have the old meaning, as I don't want to break old Lua code.
All Lua 5.3 programs would be runnable on Lua 5.4 VM without modifications.
But in 5.4 new incompatible syntax {...#} would exist for new semantics.

All the discussions about Lua table length issues are in the context of arrays.
Table length is undefined for dictionaries.
IMO, only sequential part should be counted for {...#} constructor.
Example: the length of non-sequence table such as
{ 1, 2, 3, [4]=4, [42]=42 #}
depending on the implementation, may be 3.
assert( #{ 1, 2, 3, [4]=4 } == 4)
assert( #{ 1, 2, 3, [4]=4 #} == 3)


Sparse array, lua 5.3:

setmetatable({1, 2, 3, [42] = 42, n = 42}, nlen)

Sparse array, "egorlua":

{1, 2, 3, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 42 #}

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.