Please: try-with-resouces Lock support!

Bruce Chapman brucechapman at paradise.net.nz
Fri Mar 4 13:14:09 PST 2011


On 5/03/2011 2:47 a.m., Reinier Zwitserloot wrote:
> To summarize the log, you can't tell the difference between generics on the
> type of a variable declaration and the lesser than operator:
>
> try (x<  y ? resourceA : resourceB) {}
>
> vs:
>
> try (x<y>  z = resourceA) {}
>
>
> An LL(k) parser can't tell the difference until its too late. A packrat/rd
> parser could, but both javac and ecj are LL(k) parsers so changing this
> would require both to be rewritten from scratch.
>
Yep, that's a good summary.
> Re-introducing the ability to use just an expression here would require some
> sort of keyword or symbol that is unambiguous. This could work:
>
> try (= expression) {}
>
> but it breaks the principle of least surprise, in that without knowing a
> heck of a lot about the capabilities of LL(k) parsers, the need for the =
> sign is a mystery.
>
> I gather from this discussion as well as the reaction to the fine work by
> Heinz Kabutz that the primary use case for expressions in ARM statements is
> locks, and this use case is not good enough, even if it is actually fast
> enough at least on server mode VMs.
>
>   --Reinier Zwitserloot

I wouldn't say the prime use case is locks. The prime use case is 
anywhere here you want to access the resource in a catch or finally, 
because the scope of the
resource declaration is only the block.


Hence

try(Resource r  = getResource()) {
     doSomething(r);
} catch (ResourceException e) {
     System.out.println(e + " from " + r); // r undefined here
}

is invalid, you need to hoist the variable r outside the t-w-r so that 
it is in scope in the catch

Resource r  = getResource();
try(Resource rDash = r) {
     doSomething(r);
} catch (ResourceException e) {
     System.out.println(e + " from " + r); // r visible
}

which would be nicer (hence what I consider the primary use case for 
expr) if we could just write

Resource r  = getResource();
try(r) { // not currently permitted
     doSomething(r);
} catch (ResourceException e) {
     System.out.println(e + " from " + r);
}

This last case is currently illegal, but allowing an identifier (as a 
very limited subset of expression) as well has not been ruled out by the 
expert group, and does not have the ll(k) problems that a general 
expression does.

Bruce



More information about the coin-dev mailing list