Scala BigDecimal breaks equals / hashCode contract?

As required Ordered trait, the method equalsin the Scala class BigDecimalis consistent with order. However, the hash code is simply taken from the wrapped one java.math.BigDecimaland is therefore incompatible with peers.

object DecTest {
  def main(args: Array[String]) {
    val d1 = BigDecimal("2")
    val d2 = BigDecimal("2.00")
    println(d1 == d2) //prints true
    println(d1.hashCode == d2.hashCode) //prints false
  }
}

I can not find a link to this known issue. Did I miss something?

+3
source share
2 answers

The people on the Scala user mailing list seem to agree that this is a mistake. I guess this has not been so far, because no one has ever used BigDecimalas a key in the hash structure. It was reported as bug # 2304

+7
source

: ! , , , .


equals/hashCode. , d1.equals(d2) . , d1.equals(d2) false. ?

, "2" - , "2.00"; . , (2 == 2.00), (0!= 2).

, , Java BigDecimal equals . , Java, , .

0
source

Source: https://habr.com/ru/post/1716281/


All Articles