Alternative syntax for closures
Rémi Forax
forax at univ-mlv.fr
Wed Jul 16 06:39:51 PDT 2008
Neal Gafter a écrit :
> On Tue, Jul 15, 2008 at 3:08 PM, Rémi Forax <forax at univ-mlv.fr
> <mailto:forax at univ-mlv.fr>> wrote:
>
> Neal Gafter a écrit :
>
> And how is your method invoked?
>
> like that:
> bolean cond=...
> myIf(cond) {
> //true part
> } {
> //false part
>
> }
>
>
> Your syntax is ambiguous. There is no way to tell if it is to be
> parsed as a single invocation
>
> IDENTIFIER ( ARGS ) BLOCK BLOCK
>
> or an invocation followed by a block
>
> IDENTIFIER ( ARGS ) BLOCK
> BLOCK
yes, the syntax is ambiguous but it can be solved by prefering the former
than the later.
>
>
> How do I get exception transparency for non-trailing arguments?
>
> There is no way.
>
>
> Then I presume there is also no way to declare local variables or
> fields to store closures that have parametric exceptions, which rules
> out a number of useful control APIs. Your proposal is not just a
> different syntax for the same semantics; it supports only a subset of
> the useful functionality of BGGA.
Not exactly.
Closure block types are used internally by the compiler but can't be
declared by a user.
If you want to store such closure in a local variable or a field, you
have to convert it
to a function type.
The idea behind is that if you store a closure block in a field (or a
final/shared local field)
you have lot of chance to get an exception at runtime.
By example, this code (BGGA syntax) may work or not
depending if you have return/break/continue
in the closure block.
Executor executor=...
...
<throws E> void fork(
final {=>void throws E} block) throws E {
executor.submit(new Runnable() {
public void run() {
block.invoke(); // need a try/catch E here
}
});
}
The same example with my syntax
void fork() block() {
final void() throws Exception b =
(void() throws Exception)block; // cast is needed here
executor.submit(new Runnable() {
public void run() {
b.invoke(); // try/catch Exception needed here
}
});
}
I don't see a difference with BGGA's functionality.
Rémi
More information about the closures-dev
mailing list