Question about Pattern-Matching for Switch

Olexandr Rotan rotanolexandr842 at gmail.com
Sun Jun 2 07:57:05 UTC 2024


Hi David! I`ve looked into compiler sources a bit, and this error is
reported in Attr class when attributing switch expressions.Basically,
compiler does not know which type to assign to switch expression, as there
is not case return types, you could think of it as switch expression of
"void" type, so it cant be nor assigned nor returned, so compiler treats it
like an error. Its not a pattern matching thing, just general
switch behaviour

On Sun, Jun 2, 2024 at 5:58 AM David Alayachew <davidalayachew at gmail.com>
wrote:

> Hello Amber Dev,
>
> Apologies for not having tested many features lately for this project (and
> others). Juggling insane workloads at work.
>
> Today is the first day I had a moment to breath in months, so I tried out
> some stuff, and I was confused.
>
> I have a Switch Expression where the input is a Sealed Type. For this
> particular Switch Expression, almost every branch of it is going to throw
> an Exception of some sort. I added all of the failure cases to the Switch
> Expression, pressed compile, and it threw the following compilation error.
>
> > InputFormat.java:161: error: switch expression does not have any result
> expressions
> >                   switch (next)
>
> Again, there is going to be one result expression, so I am not blocked by
> this compilation error.
>
> I was just really surprised to see an intentional road block there.
>
> Why is there a compilation error if all of the arms of a Switch Expression
> are just throwing Exceptions?
>
> Thank you for your time and help!
> David Alayachew
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20240602/25fe02d6/attachment.htm>


More information about the amber-dev mailing list