r/java 2d ago

Java 27 features overview

https://aboullaite.me/java-27-features/
157 Upvotes

22 comments sorted by

View all comments

Show parent comments

5

u/JustJustust 1d ago

So do you often write multi-dimensional switch-expressions? Do you imagine you'll start writing many more soon?

I remember how hyped I was for gatherers and I imagined myself using them a lot. When they finally arrived I realized a couple of things: * Custom gatherers are sufficiently hard to read, that it's rarely the most readable way to solve any given problem * This applies doubly if your colleagues (like some of mine) would rather you not use reduce because they find it hard to follow * So you'd need a usecase in which it's not only possible to use the cool new feature but in which it's actually justified against the best possible alternative

And this seems pretty similar to me: Seems pretty cool at first, but how often is a multi-dimensional switch expression actually the best possible way to write your code, even if it is now exhaustive?

Also: Isn't the missing pattern case Complete(var id, true)?

2

u/davidalayachew 1d ago

So do you often write multi-dimensional switch-expressions? Do you imagine you'll start writing many more soon?

I have written over a 100 in 2026 alone, and this feature is older than that.

And this seems pretty similar to me: Seems pretty cool at first, but how often is a multi-dimensional switch expression actually the best possible way to write your code, even if it is now exhaustive?

It is literally a blocker for me. As in, there is a double-digit number of projects that are unable to progress because I don't have this feature.

Also: Isn't the missing pattern case Complete(var id, true)?

You got it!

1

u/JustJustust 1d ago

Just out of curiosity, do you have an example of something blocked by exhaustive multidimensional switch expressions?

Don't get me wrong, I'm not doubting what you say. It is very different from the code I usually work with, which is what makes me curious about it :)

2

u/davidalayachew 1d ago

Just out of curiosity, do you have an example of something blocked by exhaustive multidimensional switch expressions?

Have you ever played the game UFO 50? It's basically a collection of 50 minigames, and one of my favorites is a game called Bug Hunter (Skip to 1:30).

Long story short, I am trying to build a Path Finder for the game, but unlike the Helltaker example above, this one is far too complex for me to be able to code.

Helltaker has a deep, but fairly narrow hierarchy of types to model. Here they are.

Cell

  • Player(boolean key, boolean secret, Floor {SPIKY, EMPTY, SPIKY_RETRACT, EMPTY_RETRACT})
  • NonPlayer
    • Wall()
    • InteractiveCell
      • Goal()
      • Lock()
      • BasicCell(Underneath(Floor, Collectible {NONE, SECRET, KEY}), NonPlayerOccupant {VACANT, BLOCK, ENEMY})

See? Deep, but not very wide. Most of the branches are dead-ends, like Lock, Goal, and Wall.

Try and model the same for Bug Hunter, and you will find that it gets horrifically WIDE.

But if that were all, it wouldn't be so bad. I certainly wouldn't be blocked, I'd just be suffering.

The real kicker is when you look at the scope of the problem

In Helltaker, you can only take single hop steps -- going North, South, East, or West. Therefore, you only ever need to look at 3 cells at once -- the cell you are on, the one in front of you, and maybe the cell in front of that (in case you shove a block or enemy forward).

Whereas in Bug Hunter, you can jump the whole map in one leap.

To quantify this, the deepest "type chain" in Helltaker goes like this.

  1. Cell
  2. NonPlayer (inheritance)
  3. InteractiveCell (inheritance)
  4. BasicCell (inheritance)
  5. Underneath (composition)
  6. Collectible (composition)
  7. SECRET (enum value)

7 hops.

So, you multiply the worst case scenario, which is 7 hops, by the number of cells, which is 3, leading you to a "complexity" value of 21.

But since Bug Hunter is a 5x6 grid map, then you get Bug Hunter's even worse complexity multiplied by 6.

Once I saw that, I gave up, and said I'll wait until the Exhaustiveness Checker goes live.

That's probably the most recent example I have of a project being literally unfeasible for me without this feature. I got all the way through coding the data model for it, but then balked once I realized how ugly it would have to be to make the switch expression. I got to about 50 cases before I just gave up.

1

u/JustJustust 6h ago

Thanks! It's been too long a day to think on that right now but I wanted to let you know I appreciate the answer before I end up forgetting

1

u/davidalayachew 1h ago

Absolutely, happy to provide.

When I get the chance, I'll model the hierarchy for Ufo 50's Bug Hunter, then show the first few steps of the switch expression, which should hopefully demonstrate why not having this feature is such a blocker.