lua-users home
lua-l archive

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


On Sat, Mar 17, 2018 at 1:16 PM, Egor Skriptunoff
<egor.skriptunoff@gmail.com> wrote:
> Hi!
>
> The following idea already occurred several times on this mailing list.
> We are solving not the problem we have.
> We actually need neither nils in tables nor array data type.
> All we need is USER-DEFINABLE TABLE LENGTH.r
>

Pratically, are you proposing a new {...#} contructor that is just
syntactic sugar for:

```
local function TableWithUserDefinedLength(...)
  local result = {...}

  setmetatable(result, {

    len = select('#',...),
    __len = function(a)
        return getmetatable(a).len
    end
  })

  return result
end

```

?

Just two notes:
1) The [...] constructor can be used instead
2) It can be strongly simplified, If the __len metamethod will be
changed to accept also a number to be plainly returned.