[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: A Question of Notation
- From: Dirk Laurie <dpl@...>
- Date: Sun, 19 Dec 2010 17:08:59 +0200
On Sun, Dec 19, 2010 at 04:16:12PM +0200, steve donovan wrote:
>
> > However, there is still __call available, which, as Luiz pointed out
> > in the slice thread on 2010-12-15, allows the syntax ls{1,4,6}.
>
> __call works well on the right-hand side, but can't be coerced into
> working on the left-hand side; indexing is the only operator that has
> that desirable property.
>
For that, you could use a two-argument call:
ls({1,4,6},{10,40,60})
setmetatable(ls,{__call =
function(self,slice,newval)
if newval==nil then
local s={}
for i,k in ipairs(slice) do s[i]=self[k] end
return s
else for i,k in ipairs(slice) do self[k]=newval[i] end
end
end })
Maybe disallow nil in newval to avoid the dreaded 'holy' arrays?
Dirk