lua-users home
lua-l archive

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


> By the way, "closure" was another Lua definition I misunderstood. I
> thought that closure is a function instance which uses external local
> variables. However this is also wrong. Lua manual says as follows:
> 
> > A function definition is an executable expression, whose
> > value has type function. When Lua pre-compiles a chunk, all
> > its function bodies are pre-compiled too. Then, whenever
> > Lua executes the function definition, the function is
> > instantiated (or closed). This function instance (or
> > closure) is the final value of the expression. Different
> > instances of the same function can refer to different
> > external local variables and can have different environment
> > tables.
> 
> So, if I understand correctly, any function instance is called closure.

Yes. The point is that, when a closure has no external variables, the
difference between the closure and the "function" (or "prototype", as
it is called in the source code) is very thin. (In fact, in the current
implementation, there is a 1-1 correspondence between closures and
functions when there are no external variables.)

-- Roberto