当我这样做时,我能够循环枚举:
object Colors extends Enumeration {
type Colors = Value
val Red, Green, Blue, Yellow = Value
}
for(e <- Colors.values) { ... }
所以我很容易创建下拉列表等。
现在我的枚举存储位值,例如:
object BitMask1 extends Enumeration {
val none = math.pow(2,0).toLong
val Green = math.pow(2,2).toLong
val Yellow = math.pow(2,3).toLong
val Black = math.pow(2,4).toLong
// etc
}
我怎样才能循环这些值?
最佳答案
不能,因为您没有调用 Enumeration
的 Value
方法,因此不会创建任何枚举案例。
Scala Enumeration
已知已损坏,我可以建议使用 ADT 方法吗?
sealed trait BitMask { def repr: Long }
object BitMask {
case object None extends BitMask { val repr = math.pow(2, 0).toLong }
case object Green extends BitMask { val repr = math.pow(2, 2).toLong }
case object Yellow extends BitMask { val repr = math.pow(2, 3).toLong }
case object Black extends BitMask { val repr = math.pow(2, 4).toLong }
// etc
}
关于scala - 如何循环枚举,为每个枚举项设置值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28418178/