繁体   English   中英

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

[英]How to pattern-match a nested class in Scala?

我已经尝试了下面的代码(相同的方法是在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

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

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

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

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

scala> :type p1.room
p1.Room

实现此功能的一种方法是使用类型选择引用Room ,即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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM