I have a problem with specifying types for enumeration values (instances of scala.Enumeration
) in functions. This originally arises from my need to serialize enumeration objects in database, but I've extracted the problematic code in the following example:
object EnumerationTypes {
class EnumerationProcessor[E <: Enumeration](enum: E, value: E#Value) {
def process: E#Value = {
value
}
}
object Enum extends Enumeration {
type EnumValue = Value
val a = Value(1, "a")
val b = Value(2, "b")
}
case class Obj(val flag: Enum.EnumValue)
def main(args: Array[String]) {
val processor = new EnumerationProcessor(Enum, Enum.a)
val obj = Obj(processor.process)
}
}
It leads to the following compilation error:
error: type mismatch;
found : EnumerationTypes.Enum#Value
required: EnumerationTypes.Enum.EnumValue
val obj = Obj(processor.process)
While this works ok:
object EnumerationTypesOk {
class EnumerationProcessor[E <: Enumeration](enum: E, value: E#Value) {
def process: E#Value = {
value
}
}
class Enum extends Enumeration {
type EnumValue = Value
val a = Value(1, "a")
val b = Value(2, "b")
}
object Enum extends Enum
case class Obj(val flag: Enum#EnumValue)
def main(args: Array[String]) {
val processor = new EnumerationProcessor(Enum, Enum.a)
val obj = Obj(processor.process)
}
}
But I don't want my code to be looks like this (first define class and then its singleton instance).
So the problem: how I can make value
type be exactly the enum.EnumValue
? While it seems impossible, because types cannot depend on concrete values, maybe there are some tricks to achieve desired effect with no additional boilerplate.