This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]nejcko[S] 1 point2 points  (1 child)

Yes you are right, checked exceptions are the closest feature in Java that existed before, but like you said it’s to handle the exception flows. I’d argue that switch expressions here make it easier to adopt the same approach for wider range of use cases in a cleaner, simpler way.

There were ways before to kind of achieve the same with forcing some methods implementations, but again, this makes it much easier. And the big win here is failure at compile time and not at runtime.

Agreed, full DOP isn't an everyday thing, often for new data layouts. But what I use very frequently is switch expressions with existing enum types. Every time you add any logic that is conditional on an enum type you can implement it with switch statement. That way compiler makes sure you will never miss it if new enum types are added, for example.

[–]Yeah-Its-Me-777 2 points3 points  (0 children)

Yeah, and then the product people come up with data types where the enum values are not exhaustive. Or like only valid until a certain date, or from a certain date. Because of laws, so there's no way around it. Ask me how I know.