[External] : Re: Record pattern and side effects

Brian Goetz brian.goetz at oracle.com
Thu Apr 21 16:20:24 UTC 2022

>> We’ve already asked one of the questions on side effects (though not sure we
>> agreed on the answer): what if the dtor throws?  The working story is that the
>> exception is wrapped in a MatchException.  (I know you don’t like this, but
>> let’s not rehash the same arguments.)
> Wrapping exceptions into a MatchException destroy any idea of refactoring from a cascade of if ... instanceof to a switch.
> I think refactoring is a use case we should support.

Wrapping exceptions thrown from dtors does not affect refactoring.

If I have:

     if (x instanceof D(P)) A;
     else if (x instanceof D(Q)) B;
     else C;

and I refactor to

     switch (x) {
         case D(P): A; break;
         case D(Q): B; break;
         default: C

Let's imagine that dtor D throws.  The wrapping happens when a 
dtor/accessor is invoked _implicitly_ as a result of evaluating a 
pattern match.  In both cases, we will wrap the thrown exception and 
throw MatchException.  In this way, both instanceof and switch are 
"clients of" pattern matching, and it is pattern matching that throws.

I don't see any destruction here.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20220421/02bb5b44/attachment.htm>

More information about the amber-spec-experts mailing list