Alternative syntax for closures
Rémi Forax
forax at univ-mlv.fr
Tue Jul 15 12:44:32 PDT 2008
Neal Gafter a écrit :
> On Tue, Jul 15, 2008 at 10:05 AM, Rémi Forax <forax at univ-mlv.fr
> <mailto:forax at univ-mlv.fr>> wrote:
>
> FileReader reader=...
> with(reader) {
> throw new AnotherException();
> }
>
> is compile like this:
>
> FileReader reader=...
> with(reader,new VVE() {
> public void invoke() throws AnotherException {
> throw new AnotherException();
> }
> });
>
> here the compiler awaits two catch blocks
> to catch AnotherException and IOException.
>
>
> I'd like to know what language rules you have in mind that would
> result in the compiler inferring that this "with" invocation can throw
> AnotherException.
The method with() is not a regular method but a method that takes a
block closure,
it has a special syntax and a lightly different semantics.
A method invocation of a method that has a block closure can throw E if
the block closure can throw E.
Because this kind of method currently doesn't exist so there is no
problem of backward compatibility.
>
> As i said, i've just proposed a new syntax, not a new semantics.
>
>
> Your handling of exceptions appears to be new semantics compared to
> BGGA, and I don't yet understand how you intend it would work.
Ok, to be crystal clear, here is the equivalent using the BGGA syntax:
<throws E>
static void with(Closeable closeable, {=> void throws E} block) throws
E, IOException{
try {
block.invoke();
} finally {
closeable.close(); // may throw an IOException
}
}
>
> -Neal
Rémi
More information about the closures-dev
mailing list