4

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?

Mario Galic
  • 47,285
  • 6
  • 56
  • 98

2 Answers2

5

This is a bug.

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

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
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
simpadjo
  • 3,947
  • 1
  • 13
  • 38
  • The question is why `expected`, `actual` are like `a`, `c` and not like `a`, `b`. – Dmytro Mitin Apr 28 '19 at 16:18
  • Very strange: I tried to add `equals`/`hashCode` to `Outer` but still does't work. But if I do it in Java it works. So Java compares outer classes by `equals` and Scala by reference. – simpadjo Apr 28 '19 at 22:16
  • 2
    Not so strange. `outer.Inner` and `outer1.Inner` are different **types** (`equals`, `hashCode` are irrelevant). If `a` and `c` have different types then auto-generated `equals` gives `false` for them. Java doesn't have path-dependent types. Java type `Outer.Inner` is like Scala type `Outer#Inner`. – Dmytro Mitin Apr 28 '19 at 22:23