arrow icon indicating copy to clipboard operation
arrow copied to clipboard

NonEmptyList breaks List equality contract

Open wfhartford opened this issue 3 years ago • 0 comments

Implementations of List generally have a equals function which allows for different list implementations to be considered equal if their contents are equal. This does not appear to be the case with the NonEmptyList.equals() function. Is this intentional or a bug?

Of the following two test cases, the first fails while the second succeeds:

  test("nel equality 1") {
    ("thing".nel() == listOf("thing")) shouldBe true
  }
  test("nel equality 2") {
    (listOf("thing") == "thing".nel()) shouldBe true
  }

The current implementation of NonEmptyList.equals() clearly requires that the other object have the same runtime class:

  override fun equals(other: Any?): Boolean {
    if (this === other) return true
    other?.let {
      if (it::class != this::class) return false
      (other as NonEmptyList<*>)
      if (all != other.all) return false
      return true
    } ?: return false
  }

wfhartford avatar Aug 23 '22 14:08 wfhartford