groovy

== operator uses compareTo instead of equals to calculate its result for Comparable classes

Details

  • Type: Sub-task Sub-task
  • Status: Open Open
  • Priority: Major Major
  • Resolution: Unresolved
  • Affects Version/s: 1.1-rc-3
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None
  • Number of attachments :
    0

Description

This is related to the GString equality issues, but is an issue on its own.

This might cause nasty side effects.

BigDecimal a = 2.0
BigDecimal b = 2.00

println a.equals(b) // false
println(a.compareTo(b)) // 0
println a == b // true

println([a].equals([b])) // true, which is different from Java, which would return false!

I think Groovy should try to behave 100 percent like Java in such contexts.

Activity

Hide
blackdrag blackdrag added a comment -

I changed the version fix version to 1.2 since this is a design issue rather than a bug and we won't change that for 1.1

Show
blackdrag blackdrag added a comment - I changed the version fix version to 1.2 since this is a design issue rather than a bug and we won't change that for 1.1
Hide
Leo added a comment -

Some related stuff: In Groovy 1.6.4 you get

assert new BigDecimal(51800, 1) == new BigDecimal(5180, 0)  // OK
assert [new BigDecimal(51800, 1)] == [new BigDecimal(5180, 0)]  // OK
assert [new BigDecimal(51800, 1)].equals([new BigDecimal(5180, 0)])  // OK
assert ["foo": new BigDecimal(51800, 1)] == ["foo": new BigDecimal(5180, 0)]  // FAIL
assert ["foo": new BigDecimal(51800, 1)].equals(["foo": new BigDecimal(5180, 0)])  // FAIL

I expected the last two lines to succeed.

Show
Leo added a comment - Some related stuff: In Groovy 1.6.4 you get
assert new BigDecimal(51800, 1) == new BigDecimal(5180, 0)  // OK
assert [new BigDecimal(51800, 1)] == [new BigDecimal(5180, 0)]  // OK
assert [new BigDecimal(51800, 1)].equals([new BigDecimal(5180, 0)])  // OK
assert ["foo": new BigDecimal(51800, 1)] == ["foo": new BigDecimal(5180, 0)]  // FAIL
assert ["foo": new BigDecimal(51800, 1)].equals(["foo": new BigDecimal(5180, 0)])  // FAIL
I expected the last two lines to succeed.

People

Vote (8)
Watch (5)

Dates

  • Created:
    Updated: