What is the current state of spec for Java closure?
In the proposed Java closure spec, would we be able to
create an array or collection of closures?
If so, would this syntax be possible?
{int x, int y => boolean b}[] comparisonSwitch = {
{int i, int j => return i>j},
{int i, int j => return j<i},
{int i, int j => return j==i}
}
boolean compare(int acase, int a, int b){
return comparisonSwitch[acase].invoke(a,b);
}
Would normal methods be considered as non-anonymous closures?
So would the following syntax be possible?
public class Asdf
{
public boolean gt(int x, int y){
return x>y;
}
public boolean lt(int x, int y){
return x<y;
}
public boolean eq(int x, int y){
return x==y;
}
{int x, int y => boolean b} GT = gt;
{int x, int y => boolean b}[] comparisonSwitch = {
gt, lt, eq
}
}
i.e., are closures and methods interchangeble operationally?
Would the following syntax be allowed?
// declare a method that has a closure type as an argument
void closurator( {String s => int a} findlen ){
// do whatever
}
String s = "hello";
void useClosurator(){
// invoke the method by supplying a non-anonymous method
// of an object
closurator(s.indexOf(String ss));
}
How would we be able to specify a closure type in an interface?
Could we do the following, effectively declaring final/constant reference to methods.
interface Closuration
{
public class Asdf
{
static public boolean gt(int x, int y){
return x>y;
}
static public boolean lt(int x, int y){
return x<y;
}
static public boolean eq(int x, int y){
return x==y;
}
}
{int x, int y => boolean b}[] comparisonSwitch = {
Asdf.gt, Asdf.lt, Asdf.eq
};
}
Since closures would access code space, just as reflection would, would use of closure slow down the performance of a programme? If not, would it mean, that reflection would be sped up by borrowing advances made in "closure technology"?
Inserted new question: Actually, would closure code be part of code space or in the variable heap, because I am predicting that closure code would be susceptible to be wiped off by garbage collection, right?
May I request you to focus on the gist of the questions, not on any syntax errors/typos/missing keywords in the example code. Any typos/errors, please correct them for me. Thank you.
Closures are the inline-function valued expressions which means that they are the class functions with bounded variables. Closures can be passed to another function as a parameter. A closure gives us access to the outer function from an inner function.
A lambda expression is an anonymous function and can be defined as a parameter. The Closures are like code fragments or code blocks that can be used without being a method or a class. It means that Closures can access variables not defined in its parameter list and also assign it to a variable.
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function's scope from an inner function.
Variable Declaration Identifiers are the names of variables. They must be composed of only letters, numbers, the underscore, and the dollar sign ($). They cannot contain white spaces.
You're asking about the JDK7 closures work, so references to javac.info are not relevant. That site is about the now-completed openjdk closures project, which showed how to add transparent closures to Java - transparency in the sense of satisfying Tennent's Correspondence Principle and described roughly in my blog.
The effort for JKD7 is organized under the openjdk lambda project. The specification is undergoing rapid evolution, so any answers are tentative. As Tom Hawtin pointed out, here is the latest draft spec.
Before answering your specific questions, it is worth observing that Java has separate namespaces for variables and methods. Consequently, there is likely to be some syntactic distinction between invoking a method and invoking a variable of function type (in C# parlance, a delegate). Similarly, it is unlikely you will be able to refer to a method by just naming it, as you would to refer to a field.
To answer your questions:
java.util.List
instead of an array. Given that the separate project Coin is considering adding Collection
literals and indexing operations for List
, it is likely to be just as syntactically convenient.Closures in JDK7 are short on detail at the moment. In the presentation at Devoxx the examples used were very similar to the FCM closures proposal.
Assuming that spec is used in JDK7 then I think the answer to parts 2, 3 and 4 of your question is yes (although I could well be wrong).
For part 1 - I think it should be possible to have arrays since method literals are assignable to Method objects.
For part 5 I would suspect the performance would be similar to inner classes.
Sorry I am being a bit vague - I hope it helps a bit. It is probably still to early to answer your questions with certainty.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With