<< Back to previous view

[CLJ-1030] Misleading ClassCastException when coercing a String to int Created: 25/Jul/12  Updated: 05/Sep/13

Status: Open
Project: Clojure
Component/s: None
Affects Version/s: Release 1.4
Fix Version/s: None

Type: Enhancement Priority: Minor
Reporter: Philipp Meier Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: errormsgs

Attachments: File improved-int-char-casting-error-messages.diff     File string-coerce-to-int.diff    
Patch: Code and Test


Observed behaviour

(int "0") =>
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Character

Expected behaviour
(int "0") =>
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

Comment by Andy Fingerhut [ 24/Sep/12 11:20 AM ]

If someone wants to improve the behavior of int in this case, they should also consider similar improvements to the error messages for unchecked-int, char, and unchecked-char.

Comment by Michael Drogalis [ 06/Jan/13 8:45 PM ]

Patch improved-int-char-casting-error-messages.diff on January 6, 2013.

Comment by Alex Miller [ 03/Sep/13 12:04 PM ]

int knows how to coerce numbers and chars to ints. It does not currently support Strings. It would be worthwhile to either add this behavior or make int's docstring more descriptive than just "Coerce to int".

Comment by Michael Drogalis [ 04/Sep/13 8:25 AM ]

September 4th, 2013
Fixes int and char casting error messages, and allows casting from String to int as Alex Miller suggested.

Comment by Michael Drogalis [ 05/Sep/13 12:39 PM ]

Notably, (map int ["1" "2" "3"]) fails with "ClassCastException java.lang.String cannot be cast to java.lang.Character" using this patch.
It emanates from RT.java from intCast(Object). So it looks like the Strings are losing their types when they're being mapped over.
I would guess this is intentional?

Generated at Sun Oct 22 05:13:36 CDT 2017 using JIRA 4.4#649-r158309.