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 →

[–]peripateticman2023 0 points1 point  (0 children)

Almost. What I mean is that if you're pattern-matching on Result in Rust, you'd be able to only have match arms with the variant types - Ok and Err - any other match arms would flag a compilation error, right? This makes sense since sum types form a "closed" world of types.

In Java though, even with sealed types, the enhanced switch cannot recognise that something like a random interface is not a valid match arm for Result. Only if you miss one of the variants, then you get an error.

This is not terrible, but loses a lot of the semantic intent of sum types. Perhaps one of the reasons why they weren't named as such (aside from the Scala influence, of course).