Search |
||
Syntax of closurePosted by forax on August 31, 2006 at 6:37 AM PDT
In forum (fora) and blogs, i have often seen that closures are compared to anonymous classes in order to reject the proposed syntax of closure. In this entry, i try to show up why the closure syntax is such as it is by explaining the differences with the anonymous class syntax. Let me introduce an example that sort a list of integers in reverse order. There are two codes, one written with an anonymous class and the other written with a closure using the closure convertion. The code using a closure:
List<Integer> list=Arrays.asList(666,7,12);
Collections.sort(list,int(Integer i1,Integer i2) {
if (i1<i2)
return 1;
else
if (i2<i1)
return -1;
return 0;
});
And the code using an anonymous class:
List<Integer> list=Arrays.asList(666,7,12);
Collections.sort(list,new Comparator<Integer>(){
public int compareTo(Integer i1,Integer i2) {
if (i1<i2)
return 1;
else
if (i2<i1)
return -1;
return 0;
}
});
I've found five differences and all have a reason so i don't see why people want to change the proposed syntax. A step by step comparison
I hope this entry will reconcile the grimpies with the proposed
syntax.
»
Related Topics >>
Open JDK Comments
Comments are listed in date ascending order (oldest first)
|
||
|
|