Scala programmers have a few options when defining enumerations:
- Use Scala Enumeration
- Mimic enumerations using Scala sealed case objects.
- Use Scalaz Enum
- Use Java Enumeration
When researching the best practices around enumerations in Scala, I stumbled upon the Google post entitled Enumerations must DIE and also this blog which highlights a potential problem using the Scala Enumeration
class. Both these references have cast a negative shadow over the Scala Enumeration
class.
Option 2 seems like a lot of work and with regards to Option 3, I have not used the Scalaz library as of yet so I would interested to know the experience others have had with using Scalaz
Enum
. The final option is to inter-op with Java which I try to avoid since I like to take a purist approach in my Scala programming.
The point of this post is to leverage the community experience in order to detail the context(s) when one option would be preferred over another and also in which context(s) would using a particular option be wrong or likely to cause serious issues, so that an informed decision can be made in choosing one option over another. I am not looking for opinions but rather concrete use context(s) when one option is better than the other(s); opinions are likely to get this post closed down so please avoid that.
match
statements. – acjay Feb 18 '15 at 17:09