加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读