They are real objects, can have associated data and methods.
So if they are surprised by Rust, haven't learnt Java properly
They are real objects, can have associated data and methods.
So if they are surprised by Rust, haven't learnt Java properly
For reference, they can be modeled as a "sealed" class since Java 17. https://openjdk.org/jeps/409
Prior to Java 17, sealed classes feature can be partially recreated via private constructors in the super classes. This approach forces you to put all classes in a single Java file, which can be awkward to navigate.
Though from a quick search, it's still just scalar/unary values. Rhough one of the better implementations of those.
But the big advantage of the more sum type Style enums rust provides is the ability to nest values in them while preserving type safety guarantees. So I still see them as miles better as a language concept.
The thing in Java that is closest to Rust enum's would be sealed classes, which was a preview feature in Java 15 and finalized and released with Java 17.
For the real deal, there are sealed classes now, modeled on how Scala does it.
Rust enums are sum types, which means that each variant can have a totally different shape and contain different data, and it's the user of the enum that supplies the data when they create an instance of a variant. This quintessential use case for sum types not possible with Java enums (although it is now sort of possible with records and sealed traits):
enum Result<T, E> {
Ok(T),
Err(E)
}