lua-users home
lua-l archive

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


> I feel awfully stupid, but could anyone explain (or give a reference 
> for) what a "closure" is?

In Lua, from the C API point of  view, a closure is an object created by
the association of a function object and a set of data values. To create
a closure,  use the lua_pushcclosure  API function. When the  closure is
called,  the associated  function  is called  and  these values  (called
upvalues) can then  be accessed directly from the top  of the stack (Lua
4.0), or with special indexes (Lua 4.1 on).

The  question was  if it  was possible  to extract  the upvalues  from a
closure without calling the closure.

For some nice uses of closures, check the Wiki or Roberto's book.

Best regards,
Diego.

---
>From FOLDOC:

closure

1. <programming>  In a reduction system,  a closure is a  data structure
that  holds an  expression and  an environment  of variable  bindings in
which that expression is to be evaluated.  The variables may be local or
global.  Closures are  used  to represent  unevaluated expressions  when
implementing functional programming languages with lazy evaluation. In a
real implementation, both expression  and environment are represented by
pointers.