Re: A question (confusion) about closure



On Sun, 4 May 2008 08:02:23 -0700 (PDT), "xahlee@xxxxxxxxx"
<xahlee@xxxxxxxxx> wrote:


Still, it troubles me deeply, how could a patently simple explanation,
a simple concept, a penetrating view, be not clearly grasped by a
gaggle of lispers? In this thread, there are 15 messages. Not one
reply, seems to indicate that what i said is correct. Almost half of
them actually pointed out that i was incorrect, and even unhelpful.
I'm greatly troubled by this. To me, this is like going into a bar,
and when the subject of 1+1 comes up, my nonchalant remark of 2
suddently made everybody stare. This can be a terrifying experience.
Like, in a bar you suddenly realized all the people around you are not
humans but aliens. Or, you realized it's a gay bar and you are not
gay. Or, they are all black and you are the only white. Or, you are
standing out like a sole genius.

You _are_ incorrect. Following some factually correct but irrelevant
information about naming conventions, you said:

"If the language hides such global vars from user,
and provide transparent ways when you create or
access them inside your function without the __
prefix, that's closure."


Conceptually, closures create a parameterized instance of a function
by capturing the values of the function's free variables. As a
technical matter, closures were specifically created to address the
problem of "non-local" lexically scoped variables. Globals are free
variables but they are not considered to be "non-local" in the lexical
sense.

There is no technical reason why closures could not also capture
globals (the top-level is a lexical scope after all), but there is a
semantic reason not to. The variables in each lexical scope can be
thought of as out-of-band communication channels for parts of the
program contained within the scope. If all free variables were to be
captured, there would be no way to communicate out-of-band data[*].
Globals, in particular, are considered to be for communication between
different parts of the program, so globals are treated specially and
are not captured by closures.
[*] some people would consider that a Good Thing(tm).

George
--
for email reply remove "/" from address
.



Relevant Pages

  • Re: A question (confusion) about closure
    ... His remarks on closures vs object-oriented systems might be general ... foo will see that new value. ... and that globals would live in __x and that other variables would be ... But his wording is vague enough that I ...
    (comp.lang.lisp)
  • Re: closures and dynamic binding
    ... closure over outer's 'n' and 'n' will be looked for in globals instead. ... what 'inner' in Steven's example would use. ... The namespace scope of 'n' in inner is determined by where inner is located in the code -- where is is compiled. ... Even without closures, the global scope of a function is the module it is defined in. ...
    (comp.lang.python)
  • Re: tuning - corrected shootout entry
    ... Mind you, you then have to return *ht* so you can inspect it. ... I antlered globals because they work differently than, say, C ... draw draw draw ... closures have worse performance than globals, ...
    (comp.lang.lisp)
  • Re: closure question
    ... but JavaScript uses lexical scope. ... as far as I know closures are /defined/ to ... anonymous functions require lexical scope and closures: ... var c1 = make_closure; ...
    (comp.lang.javascript)
  • Re: advantages of forth over other languages
    ... Andreas Klimas schrieb: ... Many techniques exist to optimize lexical scope ... and closures -- there has been extensive research on this topic since ...
    (comp.lang.forth)