<?xml version="1.0" encoding="windows-1252"?>
<node id="262827" title="Re^4: A Real Closure (definition?)" created="2003-06-03 18:58:03" updated="2005-08-07 11:58:03">
<type id="11">
note</type>
<author id="22609">
tye</author>
<data>
<field name="doctext">
&lt;blockquote&gt;
&lt;blockquote&gt;I think that the important thing about closures is being able to call the same code but have it use different variables (without passing them in as arguments).
&lt;/blockquote&gt;&lt;em&gt;
No. You are just confusing a useful use of closures for a definition of what they are.
&lt;/em&gt;&lt;/blockquote&gt;&lt;p&gt;
"No"?  So I don't think that that is the important thing about closures?  Then what do I think?  I'd like to know. q-:
&lt;/p&gt;&lt;p&gt;
Find me a web page that contains a definiton for "closure" in terms of generic computer programming (best if it is not specific to just one programming language).  I've looked for such a couple of times before and again recently and I haven't found anything even close.  &amp;#91; I'm quite familiar with the term "closure" from set theory (mathematics) -- as well as from popular "psychology" and for zippered plastic, food-storage bags. (: &amp;#93;
&lt;/p&gt;&lt;p&gt;
I've heard several definitions of "closure" and been told that several of them were wrong (including the one that I based my assertion several layers further up in this thread upon).  But I've never seen any kind of authoritative definition of the term.
&lt;/p&gt;&lt;p&gt;
If your language either doesn't have lexical variables or it doesn't have nested subroutines, then it doesn't make sense to talk about closures in regard to that language.  For example, Perl 4 doesn't have lexical variables.
&lt;/p&gt;&lt;p&gt;
If your language doesn't have lexical variables outside of functions, then your definition makes sense.  But based on your definition, C has closures:
&lt;code&gt;
#include &lt;stdio.h&gt;

// Can't use x here since isn't available in this scope.
// Hence, x is lexically scoped.

static int x;

void closure()
{
    return x;
}
&lt;/code&gt;
But I think people would be shocked to hear that C has closures. *shrug* Perhaps some would counter this by saying "x isn't really a lexical variable".  Whatever.  I don't care to split hairs on either definition.
&lt;/p&gt;&lt;p&gt;
So I could see defining closures as you have.  I don't find that definition very satisfying.
&lt;/p&gt;&lt;p&gt;
If you were really replying to my post from a couple of years ago (further up in this thread), then you should know that I don't assume the original definition of "closure" that I was given is correct anymore.
&lt;/p&gt;&lt;p&gt;
However, I do still think that there are a lot of things the people call "closures" for which the term isn't very meaningful.  Among these I include:
&lt;ul&gt;&lt;li&gt;Perl CODE references that refer to a subroutine that doesn't use any lexical variables from outside of itself
&lt;/li&gt;&lt;li&gt;Subroutines that use lexical variables from an outer non-subroutine scope
&lt;/li&gt;&lt;/ul&gt;
&lt;/p&gt;&lt;p&gt;
And I do still think that the &lt;i&gt;important&lt;/i&gt; thing about closures is that you can use them to hide &lt;i&gt;different&lt;/i&gt; lexicals into seperate refences to the same subroutine.  And with your definition, I can have closures without being able to do this (if I have a language that doesn't have nested subroutines or a language without code references/pointers, for example).
&lt;/p&gt;&lt;p&gt;
If one is &lt;i&gt;implementing a language&lt;/i&gt;, then one probably thinks about closures in different ways than I usually do.  Certainly, some of the more interesting statements I've heard about what &lt;i&gt;is&lt;/i&gt; a closure have come from people digging into the internals for Perl.
&lt;/p&gt;&lt;p&gt;
So I don't really care what the &lt;i&gt;definition&lt;/i&gt; of "closure" is (or even if there is even a solid consensus on what that is).  I'm glad I know a lot more (than a couple of years ago) about how far apart different people seem to draw their lines in the sand between what is and what isn't a closure.  Perhaps a lot of these people were just misinformed Perl hackers feeding off of each other (I certainly was for a while).
&lt;/p&gt;&lt;p&gt;
Perhaps you'll feel better if you just think "Perl closure" when you see that I have written "closure" on PerlMonks.
&lt;/p&gt;&lt;p&gt;
Best of all, maybe I'll get a link to some authoritative material on the term "closure" out of this. :)
&lt;/p&gt;
 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; - [tye]</field>
<field name="root_node">
95940</field>
<field name="parent_node">
262758</field>
</data>
</node>
