Rich enumeration in Scala

不打扰是莪最后的温柔 提交于 2019-12-07 12:43:21

问题


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.



来源:https://stackoverflow.com/questions/19265007/rich-enumeration-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!