[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: switch statement
- From: lhf@... (Luiz Henrique de Figueiredo)
- Date: Tue, 8 Dec 1998 14:48:14 -0200 (EDT)
Here is a nicer implementation:
function switch(t)
t.case=function (self,x)
local f=self[x] or self.default
if f then
if type(f)=="function" then
f(x,self)
else
error("case "..tostring(x).." not a function")
end
end
end
return t
end
a=switch{
[1]=function (x) print(x,10) end,
[2]=function (x) print(x,20) end,
default=function (x) print(x,0) end,
}
a:case(2)
a:case(9)
--lhf