lua-users home
lua-l archive

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

Dirk Laurie wrote:
> My own suggestion, totally ignored so far:
>    Implement 'has' via a new metatable field, whose behaviour depends
>    on its type.
>    - nil or none: the current behaviour
>    - nonnegative integer: value of #tbl, positive numeric keys being
>      legal only if in the range 1..#tbl
>    - table: a prototype containing legal keys
>    - function: a decision procedure for establishing legality

I think that arrays address the OP's point well, and it's a simple and elegant
solution. As for your solution: the first two points are equivalent to using
arrays (or a t.n field at least); for the third point, wouldn't it be enough
to extend arrays with a __pairs method?

  __pairs = function (t)
    local i = 0
    local nextp = function (t) -- closure on i
      local has = t.has -- holds legal keys
      if i < #has then
        i = i + 1
        local k = has[i]
        return k, t[k]
    return nextp, t

I'm afraid I'm missing something with the fourth point though: how would the
__has function help storing nils? For instance, how would you implement
__ipairs and __pairs if __has is a function?


Computers are useless. They can only give you answers.
                -- Pablo Picasso

Luis Carvalho (Kozure)
lua -e 'print(((""):gsub("(%u+%.)","")))'