Is there currently a way to remove or deprecate co...
# language-evolution
h
Is there currently a way to remove or deprecate comparison for enum classes? I have an enum class that I don't want comparison operators being used on, and instead all possibilities should be listed explicitly, but I can't think of a way this can currently be done. If not, I think it would be nice to have.
k
I don't think there is currently any way to disable the comparison operators at compile time (since enums are subclasses of
Enum
which implements
Comparable
). As a workaround, instead of enums you can create classes implementing a sealed interface.
h
Yeah those just don't serialize nearly as well, which is more important in my case.
r
Those two things kind of go hand in hand. Enums are easy to serialize because they have a defined ordinal value, and the ordinal value also makes them comparable.
h
No enums are serialized by name
r
> No enums are serialized by name I guess they can be, but that's not a given. I generally just use the ordinal to serialize them, so I honestly didn't think about using the name instead.
> Yeah those just don't serialize nearly as well I'm not sure what you're doing for serialization (or your overall use case for that matter), so take this with a grain of salt, but setting up a custom serializer for a sealed class is usually pretty simple and a one time cost, so if the comparability of enums is problematic, it's probably worth converting to a sealed class instead.
e