Scala:匹配案例类

发布于 2024-12-11 04:03:44 字数 830 浏览 0 评论 0原文

以下代码声称 Jack 受雇于建筑业,但 Jane 是经济不景气的另一个受害者:

abstract class Person(name: String) {

  case class Student(name: String, major: String) extends Person(name)

  override def toString(): String = this match {
    case Student(name, major) => name + " studies " + major
    case Worker(name, occupation) => name + " does " + occupation
    case _ => name + " is unemployed"
  }
}

case class Worker(name: String, job: String) extends Person(name)

object Narrator extends Person("Jake") {
  def main(args: Array[String]) {
    var friend: Person = new Student("Jane", "biology")
    println("My friend " + friend) //outputs "Jane is unemployed"
    friend = new Worker("Jack", "construction")
    println("My friend " + friend) //outputs "Jack does construction"
  }
}

为什么匹配无法识别 Jane 作为学生?

The following code claims that Jack is employed in construction, but Jane is yet another victim of the rough economy:

abstract class Person(name: String) {

  case class Student(name: String, major: String) extends Person(name)

  override def toString(): String = this match {
    case Student(name, major) => name + " studies " + major
    case Worker(name, occupation) => name + " does " + occupation
    case _ => name + " is unemployed"
  }
}

case class Worker(name: String, job: String) extends Person(name)

object Narrator extends Person("Jake") {
  def main(args: Array[String]) {
    var friend: Person = new Student("Jane", "biology")
    println("My friend " + friend) //outputs "Jane is unemployed"
    friend = new Worker("Jack", "construction")
    println("My friend " + friend) //outputs "Jack does construction"
  }
}

Why does the match fail to recognize Jane as a Student?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自由如风 2024-12-18 04:03:44

我相信这里发生的是 Student 案例类是在 Person 内部声明的。因此,toString 中的 case Student 将仅匹配属于特定 Person 实例的 Student

如果将案例类 Student 移动到案例类 Worker 并行(然后从其中删除不必要的 extends Person("Jake") object Narrator ...它的存在只是为了让新学生最终成为特定于Jake的Person$Student),你会发现简确实是学生物学的。

What I believe is happening here is that the Student case class is being declared inside of Person. Hence the case Student in the toString will only match Students that are part of a particular Person instance.

If you move the case class Student to be parallel to the case class Worker (and then remove the unnecessary extends Person("Jake") from object Narrator ... which is only there so that the new Student wound up being a Person$Student specific to Jake), you will find Jane does indeed study biology.

画中仙 2024-12-18 04:03:44

Emil 是完全正确的,但这里有一个例子可以清楚地说明:

scala> case class A(a: String) {
     |   case class B(b: String)
     |   def who(obj: Any) = obj match {
     |     case B(b) => println("I'm A("+a+").B("+b+").")
     |     case b: A#B => println("I'm B("+b+") from some A")
     |     case other => println("Who am I?")
     |   }
     | }
defined class A

scala> val a1 = A("a1")
a1: A = A(a1)

scala> val a2 = A("a2")
a2: A = A(a2)

scala> val b1= a1.B("b1")
b1: a1.B = B(b1)

scala> val b2 = a2.B("b2")
b2: a2.B = B(b2)

scala> a1 who b1
I'm A(a1).B(b1).

scala> a1 who b2
I'm B(B(b2)) from some A

更准确地说,这行:

case Student(name, major) => name + " studies " + major

确实意味着

case this.Student(name, major) => name + " studies " + major

不幸的是,虽然 Jane 是在 Jake 上实例化的,但在 Jane 的情况下 this 指向 Jane 本人。

Emil is entirely correct, but here's an example to make it clear:

scala> case class A(a: String) {
     |   case class B(b: String)
     |   def who(obj: Any) = obj match {
     |     case B(b) => println("I'm A("+a+").B("+b+").")
     |     case b: A#B => println("I'm B("+b+") from some A")
     |     case other => println("Who am I?")
     |   }
     | }
defined class A

scala> val a1 = A("a1")
a1: A = A(a1)

scala> val a2 = A("a2")
a2: A = A(a2)

scala> val b1= a1.B("b1")
b1: a1.B = B(b1)

scala> val b2 = a2.B("b2")
b2: a2.B = B(b2)

scala> a1 who b1
I'm A(a1).B(b1).

scala> a1 who b2
I'm B(B(b2)) from some A

To be more precise, this line:

case Student(name, major) => name + " studies " + major

really means

case this.Student(name, major) => name + " studies " + major

Unfortunately, while Jane was instantiated on Jake, this in Jane's case is pointing to Jane herself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文