问题
I saw the following solution for an enum somewhere
object Day {
val values = new ArrayBuffer[Day]
case class Day(name:String) {
values += this
}
val MONDAY = new Day("monday")
val TUESDAY = new Day("tuesday")
}
This demonstrates what I am trying to go for EXCEPT there is a var hidden in the ArrayBuffer....that is kind of icky.
What I really want is a val lookupTable = Map() where when a request comes in, I can lookup "monday" and translate it to my enum MONDAY and use the enum throughout the software. How is this typically done. I saw sealed traits but didn't see a way to automatically make sure that when someone adds a class that extends it, that it would automatically be added to the lookup table. Is there a way to create a scala enum with lookup table?
A scala Enumeration seems close as it has a values() method but I don't see how to pass in the strings representing the days which is what we receive from our user and then translate that into an enum.
thanks, Dean
回答1:
Updated to show you how to rename an enum val, add additional constructor parameters, and how to add additional methods to your enum.
object Currency extends Enumeration {
sealed case class CurrencyVal(name: String, value: Int) extends Val(name) {
def *(n: Int): Int = n * value
}
val PENNY = CurrencyVal("penny", 1)
val NICKLE = CurrencyVal("nickle", 5)
val DIME = CurrencyVal("dime", 10)
val QUARTER = CurrencyVal("quarter", 25)
override def withName(name: String): CurrencyVal = {
super.withName(name).asInstanceOf[CurrencyVal]
}
}
If you don't override withName
then you get back a Currency.Value
and not a Currency.CurrencyVal
.
回答2:
I saw sealed traits but didn't see a way to automatically make sure that when someone adds a class that extends it, that it would automatically be added to the lookup table. Is there a way to create a scala enum with lookup table
I made a small lib here that might help, called Enumeratum. Its usage is as follows:
import enumeratum._
// entryName is for serdes, customise it as you'd like
sealed abstract class Day(val entryName: String) extends EnumEntry
case object Day extends Enum[Day] {
// This will add Day entries declared below into a map at compile time, automagically
val values = findValues
case object MONDAY extends Day("monday")
case object TUESDAY extends Day("tuesday")
// etc
}
Day.withName("monday") // => returns MONDAY
来源:https://stackoverflow.com/questions/26492941/scala-enumeration-with-constructor-and-lookup-table