|
|
||
Rémi Forax's BlogJuly 2008 ArchivesAlternative syntax for BGGA closurePosted by forax on July 07, 2008 at 06:12 AM | Permalink | Comments (17)In my last blog entry, i've said it was time to discuss about the BGGA closure syntax. So here is my proposed syntax. The closure syntax
There are two parts in a closure syntax, the first one defines the
closure type, the second one defines the expression
(not a statement) of the closure.
By example, with a BGGA closure Why BGGA syntax is not great In my opinion for multiple reasons:
Ok, enough critics. Let's be constructive. My proposed syntax expression closure = closure_parameters expr closure_parameters = '|' parameters lists '|' Examples:
RemisClosure = |int a, int b| a + b;
Integer[] primes = { 19, 23, 2, 11, 17, 31, 5, 13 };
Arrays.sort(primes, |Integer x, Integer y| y.compareTo(x));
The syntax is close to Ruby's one,
as I previously said, closures are expressions because in Java,
Closure in other languages:
BGGAClosure = {int a, int b => a + b };
GroovyLikeClosure = {int x, int b -> a + b };
RubyLikeClosure = { |int a, int b| a + b };
FanClosure := |int a, int b -> int| { return a + b }
Gotcha: closure with no parameter type
In Java
Function type In Java, return type is declared before parameter type, I don't see why function type should break that rule. So a function type is a return type followed by parameter types separated by comma and enclosed in parenthesis. This syntax is very similar to FCM one. Update ask by Neal, grammar of function type:
return_type '(' parameter_type_list? ')'
Example:
f(2, |int x| x+2);
...
static int f(int value, int(int) closure) {
return closure.invoke(value);
}
g(|int x| |int y| x+y);
...
static int g(int (int ()) closure) {
return closure.invoke(2).invoke(3);
}
int (char[]) throws IOException read =
reader#read(char[]);
int lengthSum=sum(|String s| s.length(), "toto", "tutu");
...
static <V> int sum(int (V) f, V... values) {
int sum=0;
for(V value:values) {
sum+=f.invoke(value);
}
return sum;
}
Closure block
A closure block is a block of instructions
that can be passed to a method
in order to perform control abstraction.
expr = method_call closure_parameters? block_body Example:
Reader reader=...
with(reader) {
...
}
...
public static void with(Closeable closeable) void f() {
try {
f.invoke();
} finally {
reader.close();
}
}
This syntax declares the closure block type after the method parameters (like in Scala). The syntax has to be different from a method that takes a function type as parameter because
method = method_decl closure_block_decl* method_body
closure_block_decl = name '(' parameters ')'
Closure block return type is always 'void',
so the syntax ommit it.
The variable containing the closure block is not useable
as left value.
static void forEach(int... nums) block(int) {
for (int n: nums) {
block.invoke(n);
}
}
...
int sum = 0;
forEach(2,3,4) |int n| {
sum += n;
}
...
static boolean hasNegativeValue(int... nums) {
forEach(nums) |int n| {
if (n<0)
return true;
}
return false;
}
I don't think a specific syntax for loop abstraction worth the need to learn a new syntax.
Conversion between function type and block type It's possible to pass a function type as argument of method that declares a closure block type. The opposite is not true because closure block can do non local transfer but you can use a cast.
void invokeInEDT() closure() {
void() f=closure; // illegal conversion
final void() f=((void())closure; // ok
EventQueue.invokeAndWait(new Runnable() {
public void run() {
f.invoke();
}
});
}
This entry is already too long, so i stop here. I wait your comments.Rémi Source of BGGA prototype availablePosted by forax on July 05, 2008 at 03:37 AM | Permalink | Comments (3)
After being frozen more than 3 months,
the BGGA closure dev is resumed,
the sources of the BGGA prototype have been pushed 3 hours ago
by Neal Gafter.
Troll: I think it's time to reconsider the syntax to be less alien or from mars. | ||
|
|