Closures

Function expressions are evaluated to closure literals. This means that the closure captures the environment at the time of declaration, not at the time of invocation. In this example, the add1 function captures the value of one at the time of declaration, not at the time of invocation.

one = 1 add1 = func(x int) int { x + one } one = nope add1(2)

In Python, the same code would raise an error because the one variable is redefined before the add1 function is called.

one = 1 add1 = lambda x: x + one one = None add1(2)

© 2024 ouzu