Clojure

= on sorted collections with different key types incorrectly throws

Details

  • Type: Defect Defect
  • Status: Reopened Reopened
  • Priority: Major Major
  • Resolution: Unresolved
  • Affects Version/s: Release 1.5, Release 1.6
  • Fix Version/s: None
  • Component/s: None
  • Labels:
  • Patch:
    Code and Test
  • Approval:
    Triaged

Description

Comparing a sorted-set with numbers to a set with keywords is not symmetric:

user=> (= #{:a} (sorted-set 1))
false
user=> (= (sorted-set 1) #{:a})
ClassCastException java.lang.Long cannot be cast to clojure.lang.Keyword  clojure.lang.Keyword.compareTo (Keyword.java:109)

The latter case should return false instead of throwing.

Cause: APersistentMap.equiv() and APersistentSet.equiv() do not expect this exception be thrown from the containsKey()/contains() check. It would probably be best for PersistentTreeMap and PersistentTreeMap to implement equiv() and handle that possibility appropriately. Should also consider similar changes in equals() if necessary.

See also: CLJ-1983 (downstream example with clojure.data/diff)

Activity

Hide
OHTA Shogo added a comment -

PersistentVector also has the same problem.

user=> (compare [1] [:a])
java.lang.ClassCastException: clojure.lang.Keyword cannot be cast to java.lang.Number

The cause of this problem is that Util.compare() casts the second argument
to Number without checking its type when the first argument is a Number.

Show
OHTA Shogo added a comment - PersistentVector also has the same problem. user=> (compare [1] [:a]) java.lang.ClassCastException: clojure.lang.Keyword cannot be cast to java.lang.Number The cause of this problem is that Util.compare() casts the second argument to Number without checking its type when the first argument is a Number.
Hide
OHTA Shogo added a comment -

Umm, my brain was not working right.
Util.compare() should raise an Exception when the arguments' type are different.

Show
OHTA Shogo added a comment - Umm, my brain was not working right. Util.compare() should raise an Exception when the arguments' type are different.
Hide
Fran├žois Rey added a comment -

Upvoting.
Here's a instance of this bug in codox:
https://github.com/weavejester/codox/issues/91

Show
Fran├žois Rey added a comment - Upvoting. Here's a instance of this bug in codox: https://github.com/weavejester/codox/issues/91
Hide
Stuart Halloway added a comment -

The behavior of get is consistent with Java collections, so I think changing that expectation should be considered a feature request and not a bug.

The fix for equals should be informed by the approach taken in the JDK, where the implementation of equals (not get) has exception catchers.

Show
Stuart Halloway added a comment - The behavior of get is consistent with Java collections, so I think changing that expectation should be considered a feature request and not a bug. The fix for equals should be informed by the approach taken in the JDK, where the implementation of equals (not get) has exception catchers.
Hide
Alex Miller added a comment -

I re-focused this ticket on just the equality aspect. The other request regarding `get` with a value of a different type is consistent with Java behavior and should be considered "as designed" - a separate enhancement ticket could be considered for that one.

user=> (def s (java.util.TreeSet.))
#'user/s
user=> (.add s 1)
true
user=> (.contains s "a")
ClassCastException java.lang.Long cannot be cast to java.lang.String  java.lang.String.compareTo (String.java:108)
Show
Alex Miller added a comment - I re-focused this ticket on just the equality aspect. The other request regarding `get` with a value of a different type is consistent with Java behavior and should be considered "as designed" - a separate enhancement ticket could be considered for that one.
user=> (def s (java.util.TreeSet.))
#'user/s
user=> (.add s 1)
true
user=> (.contains s "a")
ClassCastException java.lang.Long cannot be cast to java.lang.String  java.lang.String.compareTo (String.java:108)
Hide
Alex Miller added a comment -

oops, sorry for the close/reopen.

Show
Alex Miller added a comment - oops, sorry for the close/reopen.

People

Vote (5)
Watch (3)

Dates

  • Created:
    Updated: