"case catch" instead of "case throws"?
Brian Goetz
brian.goetz at oracle.com
Mon Feb 19 16:45:31 UTC 2024
No :)
It was discussed why not to use bare "catch" on the EG list. "Case
catch" is slightly different, but I'd like to explain why this is not in
the spirit of the feature.
To recap one of the reasons we rejected bare `catch`: because it is
asking a question _about the selector_ (did evaluating the selector
throw), not about the _block_ (did something in this switch block
throw.) If we called it catch, this would surely be confusing.
The essence of switch is "evaluate a thing, then do one of the following
things as a result." With an exhaustive switch with no fall through,
its "exactly one thing"; let's go with that as our model: evaluate a
thing, then do exactly one of the following things.
We pick which thing to do based on case selectors. Currently we have
case selectors for constants, patterns, and catch-all (default). Each
of these selectors should refer back _to the computation_ that is the
switch selector. A `case <constant>` says "did it evaluate to this
constant." A `case <pattern>` says "did it evaluate to something that
matches this pattern." A `case throws <pattern>` says "did it throw
something that matches this pattern." But "case catch" would be asking
"did it evaluate to something that catches this exception", which
doesn't really even make sense.
Further, I know we are all used to the word "catch" from try-catch, but
"catch" is a very imperative framing, which comes from a day where
language control constructs were all statements (while, for, if, try,
etc), not expressions. Over the last 30 years we've learned that
statements suck. (Because, expressions compose, and statements don't,
and composition is the best tool we have.)
So I think the desire to hold onto "catch" here (in addition to the
problems discussed on the EG list) is mostly skating to where the puck
*was*, rather than where it is *going*.
On 2/19/2024 9:18 AM, Patrick Doyle wrote:
> In Brian's "Uniform handling of failure in switch", he proposes the
> syntax "case throws". I see the motivation (it certainly reads nicely)
> but since such clauses are catching exceptions, wouldn't it be more in
> line with the rest of the language to use "case catch"?
>
> This would also serve to preserve the word "throws" for its usual
> purpose if ever "switch ... throws" becomes a thing.
>
> --
> Patrick Doyle
> p.r.doyle at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20240219/dbcb0597/attachment.htm>
More information about the amber-dev
mailing list