I’ve switched from using constants for Strings to using enums. Many times I need the String representation of the enum (for logging, to pass to an API that requires a String, etc).
With String constants, you can just pass the String value, but with enums you have to call toString(). Is there a way you can “default” to the String value when supplying the enum variable?
As many posters have commented, perhaps one shouldn’t be using enums if you frequently need the String representation.
You should fully adopt
enumand not use theStringrepresentation in the first place. Only use thetoString()to generate messages, logs, etc, but internally you’d want to work withenum. AnyMap,Set, etc operations should be done on theenumtype, which guarantees proper serialization, singleton pattern, and everything else thatenumtypes are supposed to have.In your snippet:
Compile-time error aside, you should really question whether the map key should even be of type
Stringin the first place. The way you’re using it, it looks like it should be aMap<Options, Something>.