scala.Enumeration doesn't let you use syntax like this
object MyEnum extends Enumeration {
val value = "string"
}
It actually requires you to do something more like
object MyEnum extends Enumeration {
val value = Value("string")
}
and then you are passing not String but MyEnum.Value.
Since you are giving up on passing around Strings (at least in Scala 2, in Scala 3 you have an option to use type MyEnum = "value1" | "value2" and/or opaque type MyEnum), you create a sealed hierarchy instead
sealed abstract class Type(
val name: String,
val number: Int
) extends Product with Serializable
object Type {
case object Type1 extends Type("type1", 5)
case object Type2 extends Type("type2", 6)
}
sealed abstract class Growth(
val name: String,
val number: Int
) extends Product with Serializable
object Growth {
case object Growth1 extends Growth("growth1", 1)
case object Growth2 extends Growth("growth2", 10)
}
def sum(`type`: Type, growth: Growth) : Int =
`type`.number + growth.number
If you needed to construct a collection of all possible values, instead of scala.Enumeration you can use Enumeratum library instead
import enumeratum.values._
// IntEnumEntry and IntEnum specialize for Int
// and it allows to eliminate boxing
// The Int has to be defined as `val value: Int`
// findValues has to called so that the compiler
// can find all instances in the compile-time and
// populate the `values` collection
// Enumeratum then generate a lot of useful methods out of
// this list e.g. `withName` to find enum by its name
sealed abstract class Type(
val value: Int
) extends IntEnumEntry
object Type extends IntEnum[Type] {
case object Type1 extends Type(5)
case object Type2 extends Type(6)
val values = findValues
}
sealed abstract class Growth(
val value: Int
) extends IntEnumEntry
object Growth extends IntEnum[Growth] {
case object Growth1 extends Growth(1)
case object Growth2 extends Growth(10)
val values = findValues
}
def sum(`type`: Type, growth: Growth) : Int =
`type`.value + growth.value
enums or libs like enumeratum. Scala 2'sEnumerationare broken.Map[String, Int]that gives the value for each of the possible strings? Then a simplefoldLeftcould be used to compute the total for a list of strings.Map.Enumerationis kinda broken in scala 2, and isn't the right tool for this use case anyhow.