4 ms·
It shows the majority of its strength in switch statements. You can mostly just use the instanceof for a one line destructuring of a compound object into its co
by BlackFly 4y ago
It shows the majority of its strength in switch statements. You can mostly just use the instanceof for a one line destructuring of a compound object into its constituents. The corresponding rust statement is the `if let` or `if case let` if you are more of a swift fan. Generally you would use it with sum types which were previously extremely nasty to work with in Java but are more useful now with sealed classes.
The pattern shows up a lot with result and option types but you can do it any other time where a tagged union makes sense. The classic way to do a tagged union in Java was just a personal taste amongst bad options: tagging interface and instanceof on implementations or an encapsulating class that will return null (or throw an exception) if the gets are called for the types which aren't encapsulated.
If in your career you never needed to enumerate cases that required different data in each case and you couldn't see how binding those requirements into an invariant via the type system was helpful then you probably won't use these new features either. Some people thought it was helpful but the boilerplate cost was too high so wouldn't do it in practice (I am in this category).
Edit: you could also make tagged unions more type safe with callbacks, but then implementing a closure to pull the value out of the callback was just annoying.
- quilombodigital 4y ago"If in your career you never needed to enumerate cases that required different data..." . Maybe one or two times... but it is not like "hey, it is impossible", or "shit, I have to write 10 more lines" in this rare code I will never put my eyes again. What I get angry is that people will start to use everywhere, in places they should not be using. because the guy did not wanted to stop five minutes to think about it. The river flows through the easiest path, but this does not means it is the smartest path.
- Twisol 4y agoUnfortunately, I've seen plenty of Java 8 code where people did not "stop five minutes to think about it". I don't think any language can protect us from ourselves. It's fair that you've found sensible ways to achieve your goals without ever needing pattern matching. Nobody should fault you for that. But can you grant that other developers might have other sensible ways to achieve other goals (or sometimes other ways to achieve the same goals!)? I think most Java developers will be familiar with the visitor pattern. In almost all cases (there are exceptions!), I detest it; I find sum types with pattern matching to be a far superior way to say what I mean. Java added `sealed` interfaces recently, so you can actually model a closed family of types. Now pattern matching closes the loop, giving you a supremely ergonomic way of dispatching over that closed family without using a visitor. Maybe it's not clear from other responses, but the part of the JEP about an "exhaustive switch" is critical here. Java statically guarantees that you've handled each member of your closed family, just like if you forgot to implement a method for a visitor.
- rileyphone 4y agoThere’s another way to solve the visitor problem: multiple dispatch. Alas, it brings along a separate host of problems that are trickier to solve.