Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: (tye)Re2: A Real Closure

by sauoq (Abbot)
on Jun 03, 2003 at 19:33 UTC ( #262758=note: print w/ replies, xml ) Need Help??


in reply to (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.

A closure is simply a function that refers to a free lexical variable. In the code { my $x=0; sub foo { $x } }
foo() is a closure; it just isn't a very useful one. In Perl, named closures are good for simulating static variables, but not much else (that I can think of.)

-sauoq
"My two cents aren't worth a dime.";


Comment on Re: (tye)Re2: A Real Closure
Download Code
Re: Re: (tye)Re2: A Real Closure
by diotalevi (Canon) on Jun 03, 2003 at 19:45 UTC

    I've also used that behaviour to get less-global shared variables. I was working around an even uglier problem and this isn't the sort of thing I'd recommend for general usage but it is another useful usage.

    { my $x=0; sub foo { $x } sub bar { $x } }
Re^4: A Real Closure (definition?)
by tye (Cardinal) on Jun 03, 2003 at 22:58 UTC
    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
        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
Node Status?
node history
Node Type: note [id://262758]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2014-09-21 08:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (168 votes), past polls