Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^4: A Real Closure (definition?)

by tye (Sage)
on Jun 03, 2003 at 22:58 UTC ( [id://262827]=note: print w/replies, xml ) Need Help??


in reply to Re: (tye)Re2: A Real Closure
in thread Unusual Closure Behaviour

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).
No. You are just confusing a useful use of closures for a definition of what they are.

"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-:

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. [ 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. (: ]

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.

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.

If your language doesn't have lexical variables outside of functions, then your definition makes sense. But based on your definition, C has closures:

#include <stdio.h> // Can't use x here since isn't available in this scope. // Hence, x is lexically scoped. static int x; void closure() { return x; }
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.

So I could see defining closures as you have. I don't find that definition very satisfying.

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.

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:

  • Perl CODE references that refer to a subroutine that doesn't use any lexical variables from outside of itself
  • Subroutines that use lexical variables from an outer non-subroutine scope

And I do still think that the important thing about closures is that you can use them to hide different 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).

If one is implementing a language, 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 is a closure have come from people digging into the internals for Perl.

So I don't really care what the definition 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).

Perhaps you'll feel better if you just think "Perl closure" when you see that I have written "closure" on PerlMonks.

Best of all, maybe I'll get a link to some authoritative material on the term "closure" out of this. :)

                - tye

Replies are listed 'Best First'.
Re: Re^4: A Real Closure (definition?)
by jordanh (Chaplain) on Nov 09, 2003 at 20:38 UTC
      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.

    Did you try FOLDOC?

    I know I'm replying to an old node here, but the FOLDOC entry was put up in 1994.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://262827]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-24 22:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found