Closure in Java – syntax differences between the three main proposals?

Three main schemes for adding closures to the Java language have been proposed:

>Bgga (bracha gafter Gosling ah é) is also called "total closure", which is also called "simplified internal course" by Gilad bracha, Neal gafter, James Gosling and Peter von der ah é > CICE (concise instance creation expressions), and by Bob Lee, Doug lea and Josh Bloch > FCM (first class methods), Stephen coleborne and Stefan Schulz

My question:

>What are the grammatical differences between the three proposals (bgga, CICE and FCM)?

Solution

This IBM paper gives a good example of the syntax difference between bgga and CICE:

Bgga proposal

Bgga proposes to create a concept of function type, in which the function has type parameter list, return type and throws clause In the bgga proposal, the square sum code looks like the code in listing 9:

Listing 9 Calculate the sum of squares using bgga off syntax

sumOfSquares = mapReduce(myBigCollection,{ Double x => x * x },{ Double x,Double y => x + y });

The code in the left brace is the = > symbol, which identifies the name and type of the parameter; The code on the right represents the implementation of the anonymous function being defined This code can refer to local variables defined within a block, parameters of a closure, or variables that create the scope of a closure

In bgga proposal, you can declare variables, method parameters and method return values as function types You can provide closures whenever you need an instance of a single abstract method class (such as runnable or callable); For closures of anonymous type, an invoke () method is provided so that you can call them with the specified parameter list

One of the main goals of bgga proposal is to allow programmers to create methods like control structures Therefore, bgga also proposes some syntax sugars to allow you to call methods that accept closures as if they were new keywords, so that you can create methods similar to lock() or foreach() and be control primitives as if they were called Listing 10 shows how to define the withlock () method according to the bgga proposal; Listings 11 and 12 show how to invoke them using standard forms and "control constructs":

Listing 10 Compile the withlock () method under bgga shutdown proposal

public static <T,throws E extends Exception>
T withLock(Lock lock,{=>T throws E} block) throws E {
    lock.lock();
    try {
        return block.invoke();
    } finally {
        lock.unlock();
    }
}

The withlock () method in listing 10 accepts a lock and a closure The return type of closure and the throws clause are general parameters; Type inference in the compiler usually allows it to be called without specifying the values of T and E, as shown in listings 11 and 12:

Listing 11 Call withlock()

withLock(lock,{=>
    System.out.println("hello");
});

Listing 12 Call withlock() using control construct shorthand

withLock(lock) {
    System.out.println("hello");
}

Like generic drugs, most of the complexity of closure under the bgga proposal is borne by library creators; It is much simpler to use the library method of receiving closures

The bgga proposal is also used to fix some transparency failures when trying to use internal class instances for the benefit of shutdown For example, the semantics of return, break and this in a code block are different from runnable (or other inner class instances) representing the same code block These opaque elements can cause confusion when migrating code to take advantage of general algorithms

CICE proposal

CICE proposal is a simpler proposal to solve the problem of too much trouble in instantiating internal class instances Instead of creating the concept of a function type, it just needs to create a more compact syntax and instantiate the instance of the internal class with an abstract method (such as runnable, callable or comparator)

Listing 13 shows the sum of squares under CICE It shows the unaryfunction and binaryfunction types used by mapreduce() The parameters of mapreduce() are anonymous classes derived from unaryfunction and binaryfunction; This syntax simply eliminates the large amount of redundancy associated with creating anonymous instances

Listing 13 Sum of square codes under CICE closure proposal

Double sumOfSquares = mapReduce(myBigCollection,UnaryFunction<Double>(Double x) { return x*x; },BinaryFunction<Double,Double>(Double x,Double y) { return x+y; });

Because the objects representing the functions passed to MapReduce () are ordinary anonymous class instances, their bodies can reference variables defined in the enclosing range; The only difference between the methods in listing 13 and Listing 7 is the verbose length of the syntax

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>