Rich enumeration in Scala

2019-05-15 11:27发布

问题:

I am looking for a mechanism to implement rich enumerations in Scala, the same way you can in Java add abstract methods to enums and implement them in the instances of of the enum.

Please note that using a sealed trait and case objects is not a solution, because I would not be able to iterate over the existing case objects, unless I mantain a list of them, which is very fragile to changes (especially by other people who don't understand what's going on there)

回答1:

Just extend Val.

object items extends Enumeration {

  val pen = Item("writing")
  val brush = Item("painting")

  final case class Item(key: String) extends Val {
    def kind: String = toString
  }
}

object Test {
  import items._
  def main(args: Array[String]) {
    println(pen.kind +" is for "+ pen.key)
    println(brush.key)
    println(items.values)
  }
}

or

object days extends Enumeration {
  case class Day(i: Int, name: String) extends Val(i, name) {
    def isWeekDay = i < 5
  }
  private def newDay() = new Day(nextId, null)
  val Mon, Tue, Wed, Thu, Fri, Sat, Sun = newDay()
  val daysOfTheWeek = values.toList.asInstanceOf[List[Day]]
}

object Test extends App {
  import days._
  println(Thu)
  println(days.values.toList mkString ",")
  for (d <- days.daysOfTheWeek) Console println s"$d is weekday? ${d.isWeekDay}"
}


回答2:

Check also what Viktor Klang suggested here: https://gist.github.com/viktorklang/1057513

It's a very safe way to construct an enum.