Sharing my experience with Switch Expressions

David Alayachew davidalayachew at gmail.com
Sun May 11 19:53:11 UTC 2025


Sorry, the flattened version I linked to included a default clause at the
bottom. Leftover from when I was debugging.

Here is one that is exhaustive without a default clause.

https://github.com/davidalayachew/HelltakerPathFinder/blob/10b7a9d5fc4f6f563a68824de9a70735c7533226/src/main/java/HelltakerPathFinderModule/HelltakerPathFinderPackage/Board.java#L512

And fun fact -- debugging my missing case was so difficult, that to speed
things up, I wrote a little script to print out all major permutations of
my cases, then used *String* pattern-matching to eliminate the cases lol.
Object pattern-matching was taking too long to debug so I had to go back to
Strings and play whack a mole that way lol. Let me know if you need more
details.



On Sun, May 11, 2025 at 3:22 PM David Alayachew <davidalayachew at gmail.com>
wrote:

> Hey Chen,
>
> Sure.
>
> Here is the old nested Switch version.
>
>
> https://github.com/davidalayachew/HelltakerPathFinder/blob/12c57dab041924192b8613075ff6966b1b159e91/src/main/java/HelltakerPathFinderModule/HelltakerPathFinderPackage/Board.java#L511
>
> And here is the new flattened switch version.
>
>
> https://github.com/davidalayachew/HelltakerPathFinder/blob/b0e8359e429967758b1ded02b818b8d46ed05b32/src/main/java/HelltakerPathFinderModule/HelltakerPathFinderPackage/Board.java#L512
>
> I specifically made these links so that they link to the commit. So, you
> can browse the files, as they were at the time of that commit.
>
> But lol, hopefully looking at these can show you the night and day
> difference that I am talking about in readability? Or the ability to forget
> a case in the nested way? Flattened way outright FORBIDS me forgetting a
> case BECAUSE record patterns in Switch demand exhaustiveness. Semantically
> speaking, I almost feel like the flattened version is more accurate of my
> true intention, and less likely to be wrong.
>
>
>
>
>
> On Sun, May 11, 2025 at 11:38 AM Chen Liang <chen.l.liang at oracle.com>
> wrote:
>
>> Hi David,
>> I wonder if your code is open for review. If you can provide your actual
>> code, I think we can have a better grasp of the whole situation.
>>
>> Regards, Chen Liang
>> ------------------------------
>> *From:* amber-dev <amber-dev-retn at openjdk.org> on behalf of David
>> Alayachew <davidalayachew at gmail.com>
>> *Sent:* Sunday, May 11, 2025 9:48 AM
>> *To:* amber-dev <amber-dev at openjdk.org>
>> *Subject:* Re: Sharing my experience with Switch Expressions
>>
>> Let me also add one thing.
>>
>>
>> I had started this project before Record Patterns had been released. So,
>> it was only incidentally that I had ended up using a set of nested Switch
>> Expressions -- I was forced to back then.
>>
>> I know for a fact that, had I been given the opportunity, I would have
>> done things flat from the beginning. Nesting switch expressions is
>> definitely not my first choice.
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20250511/e4175da4/attachment.htm>


More information about the amber-dev mailing list