Scala - 不是 case class 也没有方法 .unapply

Scala - not a case class nor does it have method .unapply

我是 Scala 的新手,在使用以下代码时遇到了一些未解决的问题:

   object exprs{
      println("Welcome to the Scala worksheet")

      def show(e: Expr): String = e match {
        case Number(x) => x.toString
        case Sum(l, r) => show(l) + " + " + show(r)
      }

      show(Sum(Number(1), Number(44)))
    }

    trait Expr {
      def isNumber: Boolean
      def isSum: Boolean
      def numValue: Int
      def leftOp: Expr
      def rightOp: Expr
      def eval: Int = this match {
        case Number(n) => n
        case Sum(e1, e2) => e1.eval + e2.eval
      }
    }

    class Number(n: Int) extends Expr {
      override def isNumber: Boolean = true

      override def isSum: Boolean = false

      override def numValue: Int = n

      override def leftOp: Expr = throw new Error("Number.leftOp")

      override def rightOp: Expr = throw new Error("Number.rightOp")
    }

    class Sum(e1: Expr, e2: Expr) extends Expr {
      override def isNumber: Boolean = false

      override def isSum: Boolean = true

      override def numValue: Int = e1.eval + e2.eval

      override def leftOp: Expr = e1

      override def rightOp: Expr = e2
    }

我收到以下错误:

错误:对象编号不是大小写class,也没有unapply/unapplySeq成员

错误: 未找到:值总和

如何解决?提前致谢

在 Scala 中 case class 就像 class 有额外的好东西 + 一些其他属性。

正常 class,

class A(i: Int, s: String)

您不能像这样创建它的实例,

val a = A(5, "five")   // this will not work

您必须使用 new 创建新实例。

val a = new A(5, "five")

现在假设我们有 case class,

case class B(i: Int, s: String)

我们可以像这样创建一个新的 B 实例,

val b = B(5, "five")

这与 case class 一起工作的原因是因为 case class 有一个 auto-created 伴随对象,它提供了几个实用程序,包括 applyunapply方法。

所以,这个用法val b = B(5, "five")实际上是val b = B.apply(5, "five")。这里 B 不是 class B 而是同伴 object B 实际上提供了 apply 方法。

类似地,Scala 模式匹配使用伴随对象提供的 unapplyunapplySeq 用于 SeqLike 模式)方法。因此,正常的 class 实例不适用于模式匹配。

假设您出于某些特定原因想要定义 class 而不是 case class 但仍想将它们与 pattern-matching 等一起使用,您可以为其提供伴随对象自己需要的方法。

class C(val i: Int, val s: String) {
}

object C {

  def apply(i: Int, s: String) = new C(i, s)

  def unapply(c: C) = Some((c.i, c.s))

}

// now you can use any of the following to create instances,

val c1 = new C(5, "five")

val c2 = C.apply(5, "five")

val c3 = C(5, "five")

// you can also use pattern matching,

c1 match {
  case C(i, s) => println(s"C with i = $i and s = $s")
}

c2 match {
  case C(i, s) => println(s"C with i = $i and s = $s")
} 

此外,由于您是 Scala 的新手,您应该阅读 http://danielwestheide.com/scala/neophytes.html 这可能是任何 Scala 初学者的最佳资源。