As for counters, there is an enumeration in Scala, but the use of closed features + derived classes seems more flexible, for example:
sealed trait MyEnum { lazy val value = 0 } case object MyEnumA extends MyEnum { override lazy val value = 1 } case object MyEnumB extends MyEnum { override lazy val value = 2 } scala> val a = MyEnumB a: MyEnumB.type = MyEnumB scala> a.value res24: Int = 2 scala> val l = List(MyEnumA,MyEnumB) l: List[Product with Serializable with MyEnum] = List(MyEnumA, MyEnumB) scala> l.map(_.value) res29: List[Int] = List(1, 2)
You can use these objects without any internal structure if you do not need to match them with anything other than your string representations:
sealed trait MyEnum case object MyEnumA extends MyEnum case object MyEnumB extends MyEnum scala> val a = MyEnumA a: MyEnumA.type = MyEnumA scala> a.toString res21: String = MyEnumA scala> val l = List(MyEnumA,MyEnumB) l: List[Product with Serializable with MyEnum] = List(MyEnumA, MyEnumB) scala> l.map(_.toString) res30: List[String] = List(MyEnumA, MyEnumB)
Ashalynd
source share