对我来说似乎没问题:
object WeekDay extends Enumeration {
type WeekDay = Value
val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value
}
> makeList(WeekDay)
res2: List[WeekDay#Value] = List(Mon, Tue, Wed, Thu, Fri, Sat, Sun)
> makeList(WeekDay.Mon)
error: inferred type arguments [WeekDay.Value] do not conform to method makeList's type parameter bounds [E <: Enumeration]
makeList(WeekDay.Mon)
^
更新回复评论:
我想有人可以覆盖Value
嵌套类而不是Val
(我只是查看了源代码并且它没有密封),但我想不出理由。请注意,通常,对于所有枚举,Value
都是相同的类型:
object Bool extends Enumeration {
type Bool = Value
val True, False = Value
}
> Bool.True.getClass
res8: java.lang.Class[_] = class scala.Enumeration$Val
> Bool.True.getClass == WeekDay.Mon.getClass
res7: Boolean = true