2

一切都从几个考虑开始:

  • 提取器是 Scala 对象,它实现了一些unapply具有某些特性的方法(直接来自 «Programming in Scala 2nd edition»,我已经检查过)
  • 对象是在静态范围内延迟初始化的单例

我尝试以案例类的形式实现一种“参数提取器”,以尝试为 SHA1 检查提供一种优雅的模式。

我想根据缓冲区检查 SHA1 列表,以匹配它们中的哪一个适用。我想写这样的东西:

val sha1: Array[Byte] = ...
val sha2: Array[Byte] = ...

buffer match {
  case SHA1(sha1) => ...
  case SHA1(sha2) => ...
  ...
}

好吧,这看起来很奇怪,但现在不要打扰。

我试图通过简单地实现这样的案例类来解决问题

case class SHA1(sha1: Array[Byte]) {
  def unapply(buffer: Array[Byte]): Boolean = ...
}

并像使用它一样

case SHA1(sha1)() =>

乃至

case (SHA1(sha1)) =>

但它不起作用:编译器失败。

然后我稍微改变了代码:

val sha1 = SHA1(sha1)
val sha2 = SHA1(sha2)
buffer match {
  case sha1() => println("sha1 Match")
  case sha2() => println("sha2 Match")
  ...
}

它可以正常工作。

问题是:

Q1 : 使用这种«提取器»有什么微妙的含义

Q2:如果最后一个示例有效,我应该使用哪种语法来避免定义临时vals?(如果任何提供的编译器的工作与匹配...案例表达式)


编辑 Aaron 提出的解决方案也不起作用。一个片段:

case class SHA1(sha1: Array[Byte]) { 
  def unapply(buffer: Array[Byte]) = buffer.length % 2 == 0
}

object Sha1Sample {
  def main(args: Array[String]) {
    println("Sha1 Sample")

    val b1: Array[Byte] = Array(0, 1, 2)
    val b2: Array[Byte] = Array(0, 1, 2, 3)
    val sha1 = SHA1(b1)
    List(b1, b2) map { b =>
      b match {
        case sha1() => println("Match") // works
        case `sha1` => println("Match") // compile but it is semantically incorrect
        case SHA1(`b1`) => println("SOLVED")  // won't compile
        case _ => println("Doesn't Match")
      }
    }
  }
}
4

1 回答 1

0

简短的回答:如果您不希望将小写标识符解释为模式变量,则需要在小写标识符周围加上反引号。

case Sha1(`sha1`) => // ...

看到这个问题

于 2013-11-20T23:28:13.707 回答