Idea: "Frozen" inner function

Michael Butscher mbutscher at gmx.de
Sun Nov 26 06:28:03 PST 2006


Steve Horne wrote:
> On Sat, 25 Nov 2006 14:41:49 +0100, Michael Butscher
> <mbutscher at gmx.de> wrote:
> 
> >Especially a closure would have direct access to the current value of 
> >local variables of the outer function (if it exists yet) which is not 
> >the case for frozen functions.
> 
> That's not the definition of closures I was taught. A closure would
> behave exactly as your frozen function does - it does not have direct
> access to the current value since it keeps a copy of the value at the
> time when the definition was 'executed'.

I could not find yet such a clear definition, e.g.

http://en.wikipedia.org/wiki/Closure_(computer_science)

says

A closure typically comes about when one function is declared entirely 
within the body of another, and the inner function refers to local 
variables of the outer function. At runtime, when the outer function 
executes, a closure is formed. It consists of the inner function's code 
and references to any variables in the outer function's scope that the 
closure needs.


which is not clear about that.


> So for instance, in Python (which does use closures)...
> 
> a = 5
> fn = lambda : a
> a = 6
> print fn()
> 
> The output should be 5, not 6 - the value when the lambda expression
> was evaluated.

Have you tried that? With Python 2.5 in an interactive session, I get:

>>> def test():
	a = 5
	fn = lambda : a
	a = 6
	print fn()

	
>>> test()
6



Michael



More information about the Digitalmars-d mailing list