Clear questions and runnable code
get the best and fastest answer
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:
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:
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