Scala closed pack __scala closures

Source: Internet
Author: User
Tags closure

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 + much/more.

51CTO Editorial Recommendations: A special topic in Scala programming languages

function to add "more" to the reference, but what is more it. 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ˆ the other side Face, 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 = < F Unction> scala> Addmore (a) Res19:int = 11 a function value (object) created at run time by this function literal is called a closure: Closu re. 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&G T var sum = 0 Sum:int = 0 scala> Somenumbers.foreach (sum = _) scala> sum Res23:int =-11 example use a cyclic way to compute the list's tired Tax The variable sum is at the periphery of the function text sum = _, and the function text adds the number to sum. Although this is a closure that changes sum during the runtime, the cumulative value of the result, 11, is still visible outside the closure.

What happens if a closure accesses some variables that have several different backups when the program is running. For example, if a closure uses a local variable for a function, and a function is called many times. Each access is using which instance of the variable.

There is only one answer that coexists with the rest of the language: the instance used is active when the closure is created. For example, here are the functions that create and return an "incremental" closure:

def makeincreaser (more:int) = (x:int) => x + more creates a new closure each time a function is called. Each closure accesses more variables that are active when the closure is created.

scala> val inc1 = makeincreaser (1) inc1: (int) => Int = < function> scala> val inc9999 = Makeincreaser ( 9999) inc9999: (int) => int = < function> call Makeincreaser (1), the bound closure that captures the value 1 as more is created and returned. Similarly, call Makeincreaser (9999) and capture the value 9999 as the closure of the more is returned. When you apply these closures to parameters (in this case, only one parameter, X, must be passed in), the result of the return depends on how more is defined when the closure is created:

Scala> Inc1 (a) Res24:int = one scala> inc9999 (ten) Res25:int = 10009

Although more in this example is a parameter that has returned a method call, there is no difference. The Scala compiler rearranged it in this case so that the captured argument continues to exist in the heap, not the stack, so it can remain outside the method call that created it. This kind of rescheduling is all about automatic care, so you don't have to worry about it. Please arbitrarily capture the variables you want: Val,var, or parameters.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.