Scala枚举类型在匹配/大小写失败
发布时间:2020-12-16 19:05:37 所属栏目:安全 来源:网络整理
导读:枚举值似乎在匹配/表达式中失败.这是工作表中发生的情况. 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"
|
枚举值似乎在匹配/表达式中失败.这是工作表中发生的情况.
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
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
