Alternative syntax for closures

Neal Gafter neal at gafter.com
Tue Jul 15 12:59:19 PDT 2008


Can you please show me how you intend one would write the equivalent of the
following BGGA method?

*<throws E> void myIf(
      boolean cond,
      {=>void throws E} truePart,
      {=>void throws E} falsePart) throws E {
  (cond ? truePart : falsePart).invoke();
}
*

Regards,
Neal

On Tue, Jul 15, 2008 at 12:44 PM, Rémi Forax <forax at univ-mlv.fr> wrote:

> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/closures-dev/attachments/20080715/e221913a/attachment.html 


More information about the closures-dev mailing list