Comment

Comment by enneff

From the succinct description of closures:

    It remembers the values of all the variables that were in
    scope when the function was created. It is then able to
    access those variables when it is called even though they may
    no longer be in scope.
No! It doesn't remember the values. It binds the variables. This is a major misstep in describing closures. The most important point: when the values of the bound variables change the closure sees the new values.

Please, fix this!

A better simple description of closures:

* You can pass a closure around as a value (to be called later).

* When you create a closure it is bound to the variables in its current scope. When you call a closure, it sees the current value of the bound variables (not their values at the time the closure was created).

Follow this with a simple example, like:

    i := 0
    inc := func() {
        i++
    }
    println(i) // prints "0"
    inc()
    println(i) // prints "1"
    inc()
    println(i) // prints "2"
parent enneff 15 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton