Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Closures are an implementation detail which allow anonymous functions (among other structures) to act as expected in statically-scoped languages.

I don't think anonymous functions have anything to do with it. Closures come into play when you are creating a function that has access to local variables from an enclosing scope. This function need not be anonymous. For example, in Lua:

    function x()
      local var = 5
      local function y()
        print(var)
      end
      return y
    end

    y = x()
    y() -- Prints 5.
At no time is the inner function anonymous. When it is created it is bound to the local variable "y" and later it is bound to the global variable "y". And yet it is a closure because it has access to the lexical scope in which it was defined.

Also, if closures were an "implementation detail" it would not make sense to say that a language has or does not have closures, only implementations of a language would have or not have them (and the language user would not be aware of whether the language implementation had closures or not).



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: