Hi On 5/23/24 20:34, Aaron Piotrowski wrote:
This solution is flawed. Not every constant is necessary an enum case. It also isn't type-safe or as nice to read as tryFrom() and from() static methods.
Having regular class constants on an enum is somewhat questionable [1], but fair enough. In that case an `instanceof ExampleEnum` check will do the trick.
But you don't need to iterate on all the enum cases, checking them in sequence, like in your suggestion.
As for the initial suggestion of implicitly backing enums: I'm strongly against that. Personally I find that backed enums are almost never useful. Serialization is more reliably performed using the case name and often there is more than one reasonable scalar representation and then you need regular methods to obtain the different representations. Singling out one of those scalar representations as the “blessed” representation to store in the backed value feels semantically incorrect to me.
Best regards Tim Düsterhus [1] Except possibly to alias an enum case for BC purposes.