To this chapter, the examples of all function texts refer only to the arguments passed in. For example, (x:int) => x > 0, the only variable used by the function body, x > 0, is x, which is defined as a function argument. However, you can also refer to variables defined elsewhere:
(x:int) => x + more//How much is it?
function to add "more" to the reference, but what is more? From the point of view of this function, more is a free variable: variable, because the function text itself does not give its meaning. In contrast, the x variable is a binding variable: bound variable, because it has a clear meaning in the context of the function: it is defined as the function's unique argument, an int. If you try to use this function text independently, the scope does not have any more definition, the compiler will complain that:
Scala> (X:int) => x + more
< Console>:5:error:not found:value more
(x:int) => x + more c4/>ˆ
On the other hand, as long as there is something called more the same function text will work correctly:
scala> var more = 1
more:int = 1
scala> val addmore = (x:int) => x + more
Addmore: (Int) => Int = < function>
scala> Addmore (a)
Res19:int = 11
The function value (object) that is created at run time according to this function text is called a closure: closure. The name originates from the "close" action performed on the function text by the binding of the capture free variable. function texts without free variables, such as (X:int) => x + 1, are called closed terms: Closed term, where the term: term refers to a small part of the source code. Therefore, the value of the function created by this function text at runtime is strictly not a closure because (x:int) => x + 1 is closed at the time of writing. But any function text with a free variable, such as (X:int) => x + More, is an open term: open term. Therefore, any function value created in accordance with (X:int) => x + in runtime will have to capture its free variable, more, the binding. Since the function value is the final product of the action to turn off this open term (x:int) => x + more, the resulting function value will contain a reference to the captured more variable and is therefore called a closure.
This example poses a problem: what happens if more is changed after the closure is created? In Scala, the answer is that the closure sees the change. As follows:
scala> more = 9999
more:int = 9999
scala> Addmore (Ten)
res21:int = 10009
Intuitively, Scala's closures capture the variables themselves, not the values that the variables point to. In contrast, the inner Java class does not allow you to access variables that can be changed in the perimeter, so it doesn't make any difference whether you capture a variable or capture the value it currently has. As shown in the previous example, the closures that were created in accordance with the (X:int) => x + More see the changes made to more than the closure. The converse is the same. Changes made by closures to capture variables are also visible outside the closure. Here is an example:
scala> val somenumbers = List ( -11, -10, -5, 0, 5, ten)
somenumbers:list[int] = List ( -11, -10, -5, 0, 5, ten)
scala> var sum = 0
sum:int = 0
scala> Somenumbers.foreach (sum = _)
scala> sum
res23:int =-11