Structural equality affected by location of case class definition after deserialisation

ぐ巨炮叔叔 提交于 2020-08-07 05:59:28

问题


Why is structural equality comparison affected, after deserialisation to case class instance, by the location of case class definition being inside or outside another class. For example, the assertion in the following snippet

package example

import org.json4s.DefaultFormats
import org.json4s.native.JsonMethods.parse

class Foo {
  case class Person(name: String)
  def bar = {
    implicit val formats = DefaultFormats
    val expected = Person(name = "picard")
    val actual = parse("""{"name": "picard"}""").extract[Person]
    assert(expected == actual, s"$expected == $actual")
  }
}

object Main extends App {
  (new Foo).bar
}

fails with

`java.lang.AssertionError: assertion failed: Person(picard) == Person(picard)`

whilst it passes if we move Person definition outside class Foo like so

case class Person(name: String)
class Foo {
  def bar = {
    ...
    assert(expected == actual, s"$expected == $actual")
  }
}

Note, in both cases, deserialisation seems to be successful, for example,

assert(expected.name == actual.name)

is satisfied irrespective of case class Person definition location.

Perhaps it is somehow affected by the implicit Manifest passed in to extract?


回答1:


This is a bug.

https://github.com/json4s/json4s/issues/564 "Deserialized inner case classes cannot be compared with case classes initialized in code"




回答2:


Looks like inner classes can't be meaningfully checked for equality since every instance holds a reference to the outer object. And these references are a part of equality check:

  class Outer {
    case class Inner(s: String)
  }

  val outer = new Outer()
  val a = outer.Inner("x")
  val b = outer.Inner("x")
  println(a==b) //true
  val c = new Outer().Inner("x")
  println(a==c) //false


来源:https://stackoverflow.com/questions/55890176/structural-equality-affected-by-location-of-case-class-definition-after-deserial

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!