sorted-set bug

20 views
Skip to first unread message

David

unread,
Aug 6, 2008, 4:36:11 PM8/6/08
to Clojure
I'm using the 20080612 jar.

(conj (sorted-set []) "a")

gives me the stack trace

java.lang.ClassCastException: clojure.lang.PersistentVector cannot be
cast to java.lang.String
java.lang.ClassCastException: clojure.lang.PersistentVector cannot be
cast to java.lang.String
at java.lang.String.compareTo(Unknown Source)
at clojure.lang.RT$2.compare(RT.java:197)
at clojure.lang.PersistentTreeMap.doCompare(PersistentTreeMap.java:
291)
at clojure.lang.PersistentTreeMap.entryAt(PersistentTreeMap.java:278)
at clojure.lang.PersistentTreeMap.containsKey(PersistentTreeMap.java:
83)
at clojure.lang.APersistentSet.contains(APersistentSet.java:28)
at clojure.lang.PersistentTreeSet.cons(PersistentTreeSet.java:61)
at clojure.lang.PersistentTreeSet.cons(PersistentTreeSet.java:19)
at clojure.lang.RT.conj(RT.java:407)
at clojure.fns.clojure.conj__9.invoke(boot.clj:41)
at clojure.lang.AFn.applyToHelper(AFn.java:186)
at clojure.lang.AFn.applyTo(AFn.java:175)
at clojure.lang.Compiler$InvokeExpr.eval(Compiler.java:2569)
at clojure.lang.Compiler.eval(Compiler.java:3780)
at clojure.lang.Repl.main(Repl.java:75)

(conj (set []) "a")
works.

Rich Hickey

unread,
Aug 6, 2008, 4:56:26 PM8/6/08
to Clojure


On Aug 6, 4:36 pm, David <dsieg...@yahoo.com> wrote:
> I'm using the 20080612 jar.
>
> (conj (sorted-set []) "a")
>
> gives me the stack trace
>
> java.lang.ClassCastException: clojure.lang.PersistentVector cannot be
> cast to java.lang.String

Sorted sets (and maps) must have comparable keys. PersistentVectors
and Strings are not comparable. There is not a total ordering across
types.

> (conj (set []) "a")
> works.

Hash sets and maps support heterogeneous keys.

Rich

David

unread,
Aug 6, 2008, 5:35:16 PM8/6/08
to Clojure
I'm missing something.

This is a piece of a larger example. I want to create an empty sorted-
set
and add strings to it. Since the only items I put into the sorted set
are strings, why is anything being compared to PersistentVector? I
expected the only comparisons to be between strings.

Shouldn't a sorted-set containing only strings be allowed?

Rich Hickey

unread,
Aug 6, 2008, 5:47:31 PM8/6/08
to Clojure


On Aug 6, 5:35 pm, David <dsieg...@yahoo.com> wrote:
> I'm missing something.
>

Yes, (sorted-set []) creates a sorted-set with a single item, an empty
vector, to which you are trying to add a string. (sorted-set) creates
an empty sorted set.

> This is a piece of a larger example. I want to create an empty sorted-
> set
> and add strings to it. Since the only items I put into the sorted set
> are strings, why is anything being compared to PersistentVector? I
> expected the only comparisons to be between strings.
>
> Shouldn't a sorted-set containing only strings be allowed?

Certainly. This will do what you want:

(conj (sorted-set) "a")

or simply (sorted-set "a")

Rich

David

unread,
Aug 6, 2008, 6:20:10 PM8/6/08
to Clojure
Gotcha. Thanks.

David

unread,
Aug 6, 2008, 9:58:56 PM8/6/08
to Clojure
(conj (sorted-set) "a")

gets the traceback:

java.lang.ClassCastException: java.lang.Character cannot be cast to
java.util.Map$Entry
java.lang.ClassCastException: java.lang.Character cannot be cast to
java.util.Map$Entry
at clojure.lang.APersistentMap.cons(APersistentMap.java:50)
at clojure.lang.RT.conj(RT.java:407)
at clojure.fns.clojure.conj__9.invoke(boot.clj:41)
at clojure.lang.AFn.applyToHelper(AFn.java:186)
at clojure.lang.AFn.applyTo(AFn.java:175)
at clojure.lang.Compiler$InvokeExpr.eval(Compiler.java:2569)
at clojure.lang.Compiler.eval(Compiler.java:3780)
at clojure.lang.Repl.main(Repl.java:75)


Stephen C. Gilardi

unread,
Aug 6, 2008, 11:53:58 PM8/6/08
to clo...@googlegroups.com
Hi David,

> (conj (sorted-set) "a")
>
> gets the traceback:
>
> java.lang.ClassCastException: java.lang.Character cannot be cast to

With Clojure SVN 992, it works for me:

Clojure
user=> (conj (sorted-set) "a")
#{"a"}
user=>

You might try doing an "svn update", check "svn status" and then in
the clojure directory:

ant clean
ant

--Steve

Reply all
Reply to author
Forward
0 new messages