What is the current state of spec for Java closure?
1.
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);
}
2.
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
}
}
3.
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));
}
4.
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
};
}
5. 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.