Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Nov 2012
    Thanked 0 Times in 0 Posts

    Question Closures - Local vs Private ?


    When I read about Closures, it's often said that we are using Closures to create private variable, that cannot be accessed outside the function.
    But isn't it that this is true for all local variables inside a function? That they cannot be accessed outside the function?

    In regards of Closures, what is the difference between a local and a private variable?


  2. #2
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Keene, NH
    Thanked 477 Times in 465 Posts
    SOME programming languages will isolate EVERY closure, not just functions if you declare the variable inside the function.

    In SOME dialects of C for example (but not all compilers do this, though OFFICIALLY they should)

    void function test() {
      if (true) { int ourvalue = 2; }
    In SOME flavors that won't even make it past the compiler. In others it creates a runtime error because the "ourvalue" was declared inside yet another {}

    Honestly I'm not a fan of the practice, since it adds an extra layer of complexity for nothing, and if you understand the low level it also induces excess memory thrashing. But I learned high level programming in Wirth family languages, ALL of which REQUIRE forward declaration and have function level scoping ONLY.

    The latest flavors of ECMAScript (the PROPER name for JavaScript now that NOBODY uses) now include a 'let' replacement for 'var' that performs the same function.

    function test() {
      if (true) { let ourvalue = 2; }
    ourvalue will be 'undefined' and because it's in a formal 'let' instead of outputting 'undefined' it will actually THROW.

    Pathetically the people who cream their panties hardest for this functionality are the same jokers who scream "never use 'with'!!!"

    Beware the use of the term 'closure' also depends on who you're talking to. Folks from Smalltalk or Wirth Familiy Languages -- Pascal, Modula, Oberon, Ada -- consider a closure to just be the closing statement of a block. (what in C syntax is the closing curly bracket, in Wirth languages is the word "end" -- JOE FORBID we use complete words for things instead of cryptic symbols). C programmers USED to talk that way but these days a relic 1960's definition (like a great many things that should have been left in the 60's) is all hot and trendy, calling a closure what I was taught is an ENclosure -- as it refers to both the opening { and the closing }.

    But no, can't possibly use the extra syllable that makes the word make more sense. See the whole deprecated vs. depreciated rubbish -- deprecated makes it sound like your talking down to a five year old or a small pet in that idiotic voice most adults INSIST on using. Who's a good boy? Yes you are!!! YES YOU ARE!!!
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts