On 5/31/07, Wesley Smith <wesley.hoke@gmail.com> wrote:
> Yes, I could, but image there are 30 or 40 of these functions. The
> constructor would get rather long. The real goal here is to pass in
> functions that describe behaviors to the constructor. Sometimes,
> these behavior functions need data from the constructor itself. I
> guess I could reorganize my constructor to take a table instead of a
> list of values and pass the table into the function instead. That
> way, I can write a small constructor and general functions outside of
> it.
>
> wes
>
> On 5/31/07, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
> > > What I'd like is f1's val upvalue to refer to constructor's local
> > > variable val. Is this possible somehow through environments?
> >
> > Can't you define 'gen1' inside the constructor?
> >
> > -- Roberto
> >
>
Here's a simple example of what I believe Roberto is suggesting. It works
well for me. Am I missing something?
function gen(val)
return function()
print(val)
end
end
f1=gen(1)
f2=gen(2)
f1()
f2()
--
Mike