lua-users home
lua-l archive

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


Note: I feel I can comment on the subject, because I have done the same
error in the past (and can do it again :-)
Ralf, you should remember to copy the subject from the message you are
replying to, it will be more informative...
And don't feel stupid, I never heard of closures before coming to this
mailing list, and I still have some trouble to grab the concept...

> > 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.

Thank you for the explaination. I love this mailing list because I learn a
lot of things, like some functionnal programming concepts to the basis of
Unicode :-)

I may be wrong, but I see a little discrepancy between your definition
(which is right regarding Lua, of course) and the FOLDOC one.

If I understand correctly, closure variables in Lua are frozen, ie. their
values when the closure is defined are fixed. The function uses these constants
as additional parameters.

Closures values as per FOLDOC point to variables, ie. the function uses the
live values at the time of execution. Or not?

Regards.

-- 
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--

GMX - Die Kommunikationsplattform im Internet.
http://www.gmx.net