[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Empty? No. Array? No. Has? Yes.
- From: Luis Carvalho <lexcarvalho@...>
- Date: Wed, 3 Jul 2013 09:24:38 -0400
Dirk Laurie wrote:
<snip>
> 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
<snip>
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]
end
end
return nextp, t
end
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?
Cheers,
Luis
--
Computers are useless. They can only give you answers.
-- Pablo Picasso
--
Luis Carvalho (Kozure)
lua -e 'print((("lexcarvalho@NO.gmail.SPAM.com"):gsub("(%u+%.)","")))'