作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
枚举值似乎在 match/case 表达式中失败。这就是工作表中发生的情况。
object EnumType extends Enumeration {
type EnumType = Value
val a, b = Value
}
import EnumType._
val x: EnumType = b //> x : ... .EnumType.EnumType = b
x match {
case a => "a"
case b => "b"
} //> res0: String = a
if (x == a) "a" else "b" //> res1: String = b
最佳答案
就像@Kevin Wright 和@Lee 刚刚说的,a
和 b
作为可变模式工作,而不是作为 EnumType
值。
修复您的代码的另一个选项是明确您正在引用来自 EnumType
的值。单例:
scala> x match { case EnumType.a => "a" case EnumType.b => "b" }
res2: String = b
关于Scala 枚举类型在匹配/大小写中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21412226/
我是一名优秀的程序员,十分优秀!