How to create an enumeration in Scala with custom values and names

There has been some back and forth about whether Scala devs should even be creating enumerations to begin with.  (https://www.wlangiewicz.com/2016/03/20/scala-case-classes-vs-enumeration/)

But in the case you wanted to, here’s how to do it:

object MyEnumeration extends Enumeration {
  type MyEnumeration = Value
  val First = Value(1, "Custom Name")
  val Second = Value(2, "Custom Name2")
  val Third = Value(3, "Custom Name3")
}

Happy Coding!

 

Leave a Reply

Your email address will not be published. Required fields are marked *