如何在Scala中对嵌套类进行模式匹配?

Geo*_*lly 9 scala pattern-matching

我已经尝试了下面的代码(相同的方法是在Scala编程之后编写的)

class Person() {
    class Room(r: Int, c: Int) {
        val row = r
        val col = c

        override def hashCode: Int =
            41 * (
                41 + row.hashCode
            ) + col.hashCode

        override def equals(other: Any) =
            other match {
                case that: Room =>
                   (that canEqual this) &&
                   this.row == that.row &&
                   this.col == that.col
               case _ => false
            }

        def canEqual(other: Any) =
            other.isInstanceOf[Room]
    }

    val room = new Room(2,1)
}

val p1 = new Person()
val p2 = new Person()

println(p1.room == p2.room)
>>> false
Run Code Online (Sandbox Code Playgroud)

经过一些分析后,我发现Scala重新定义了Room每个实例的类,Person这就是两个房间不相等的原因.

解决问题的一种可能性是将类放在类之外Person,但这并不总是最简单的.(例如,如果类必须访问某些参数Person.)

有什么选择来写同等方法?

Ben*_*mes 15

问题是你的两个房间是路径依赖类型的实例:它们的类型是p1.Roomp2.Room:

scala> :type p1.room
p1.Room
Run Code Online (Sandbox Code Playgroud)

使这项工作的一种方法是参考Room使用类型选择,即as Person#Room.

class Person() {
    class Room(r: Int, c: Int) {
        val row = r
        val col = c

        override def hashCode: Int = // omitted for brevity

        override def equals(other: Any) =
            other match {
                case that: Person#Room =>
                   (that canEqual this) &&
                   this.row == that.row &&
                   this.col == that.col
               case _ => false
            }

        def canEqual(other: Any) =
            other.isInstanceOf[Person#Room]
    }

    val room: Room = new Room(2,1)
}

val p1 = new Person()
val p2 = new Person()

scala> p1.room == p2.room
res1: Boolean = true
Run Code Online (Sandbox Code Playgroud)