mistaking switch expressions and switch statements
Remi Forax
forax at univ-mlv.fr
Sat Feb 5 11:09:42 UTC 2022
----- Original Message -----
> From: "Mateusz Romanowski" <romanowski.mateusz at gmail.com>
> To: "cay" <cay at horstmann.com>
> Cc: "amber-dev" <amber-dev at openjdk.java.net>
> Sent: Saturday, February 5, 2022 11:58:34 AM
> Subject: Re: mistaking switch expressions and switch statements
> Hi Cay,
> Actually, my initial concern was about that figure's mislabeling.
>
> As for the fall-through, it has become so complicated that I had to use
> `javap` to confirm lack of fall-through.. lots of axis, indeed.
Yes, we know that there are several cases were a warning may be necessary,
default not at the end, fallthrough with no default, switch on enums not total, etc
At the same time, we don't want to have too many of them.
>
> Cheers,
> Mateusz
regards,
Rémi
>
> On Saturday, February 5, 2022, Cay Horstmann <cay.horstmann at gmail.com>
> wrote:
>
>> The article has other issues. Figure 6 is captioned "Handling complex
>> situations with a switch expression" but contains a switch *statement*
>> without fall-through.
>>
>> Another example where the two axis design (statement vs. expression,
>> fall-through vs. no fall-through) has not been properly understood.
>>
>> Cheers,
>>
>> Cay
>>
>> On 04/02/2022 22:59, Brian Goetz wrote:
>>
>>> You are correct that the default can be elided here, because this is a
>>> switch statement on a legacy target type whose labels are all constants,
>>> and such statements (for reasons of backward compatibility) can be
>>> partial. All other switches (new target types, new cases, expressions)
>>> should be total.
>>>
>>> The example actually employs "belts and suspenders"; it initializes
>>> `discount` to zero, and then assigns it to zero in the default clause. If
>>> discount were left blank, then the default clause would be necessary, not
>>> to make the switch total, but to make `discount` definitely assigned. So
>>> you could either elide the initializer on `discount`, or the default
>>> clause, but not both.
>>>
>>> On 2/4/2022 4:31 PM, Mateusz Romanowski wrote:
>>>
>>>> Hi,
>>>> I am reading article "Design implications of Java’s switch statements and
>>>> switch expressions" [1] by Vasily STRELNIKOV .
>>>>
>>>> I am confounded by "*Figure 6.* Handling complex situations with a Switch
>>>> Expression" which I believe is a new-style Switch Statement as the
>>>> `default
>>>> clause` is removable even though Switch is not total.
>>>>
>>>> Could you confirm my interpretation?
>>>> And should it be correct, help to contact author to ask for correction.
>>>>
>>>> Cheers,
>>>> Mateusz Romanowski
>>>>
>>>> [1]
>>>> https://blogs.oracle.com/javamagazine/post/java-switch-state
>>>> ments-expressions
>>>>
>>>
>>>
>> --
>>
>> Cay S. Horstmann | http://horstmann.com | mailto:cay at horstmann.com
More information about the amber-dev
mailing list