Scala Enumeration

General Question About Usability Of Scala Enumeration Class.

Is this scala construct of any real use? When comparing Enumeration to its counterpart in Java I find that Scala’s Enumeration class appears to be very weak and maybe not even needed in the language. Hence, perhaps it should be removed from the language completely. However, I am still pretty much a novice at Scala so my observation could very well be incorrect. I would be curious as to what an advanced Scala developer might have to say about this.

In Scala 2.x Enumeration isn’t a language feature but a library. Scala 3 will change this, https://dotty.epfl.ch/docs/reference/enums/enums.html.

Yeah – nearly everybody agrees that Scala 2 Enumeration is fairly useless, and I don’t often see it used. There are better approaches for the time being (I am partial to Enumeratum, which works well), and Scala 3 will have much more useful enumerations built in (see the link that Mark posted for details)…

1 Like

+1 for Enumeratum, though you should consider becoming familiar with sealed ADTs and case objects first, since a sealed set of case objects is the common approach in Scala, and Enumeratum is basically macro sugar for the same.

I think we’re all looking forward to Scala 3 enums. It’s embarrassing that java enums have been better all this time.

1 Like

I have several times seriously suggested to people to plop in a Java file containing the enum they want into a Scala codebase, the syntax is that much better…

Java Enums are similar to Scala object. Both are singleton objects. And yes, Scala objects can be used in a way similar to Java enums. Currently, I find Java enums to be a good and interesting way of using constants in your code for clarity. Java enums are very similar to java classes in that you can also implement interfaces with enums. YOu can declare abstract enums ( I think ) and inherit them in subclass enums. Within an enum, you can do a lot of interesting things such as declare classes, both concrete and abstract. Not sure how deep one would want to go into java enums because they can become a limiting factor in your code if you are not careful.