Вопрос

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)

Это было полезно?

Решение

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}"
}

Другие советы

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

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top