Clojure

Misleading ClassCastException when coercing a String to int

Details

  • Type: Enhancement Enhancement
  • Status: Open Open
  • Priority: Minor Minor
  • Resolution: Unresolved
  • Affects Version/s: Release 1.4
  • Fix Version/s: None
  • Component/s: None
  • Labels:
  • Patch:
    Code and Test

Description

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
or 
IllegalArgumentException

Activity

Hide
Andy Fingerhut added a comment -

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.

Show
Andy Fingerhut added a comment - 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.
Stuart Halloway made changes -
Field Original Value New Value
Issue Type Defect [ 1 ] Enhancement [ 4 ]
Hide
Michael Drogalis added a comment - - edited

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

Show
Michael Drogalis added a comment - - edited Patch improved-int-char-casting-error-messages.diff on January 6, 2013.
Michael Drogalis made changes -
Michael Drogalis made changes -
Patch Code and Test [ 10002 ]
Hide
Alex Miller added a comment -

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".

Show
Alex Miller added a comment - 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".
Alex Miller made changes -
Description 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
or
IllegalArgumentException
Observed behaviour

{code}
(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
or
IllegalArgumentException
{code}
Labels bug
Alex Miller made changes -
Labels errormsgs
Hide
Michael Drogalis added a comment -

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

Show
Michael Drogalis added a comment - string-coerce-to-int.diff September 4th, 2013 Fixes int and char casting error messages, and allows casting from String to int as Alex Miller suggested.
Michael Drogalis made changes -
Attachment string-coerce-to-int.diff [ 12233 ]
Hide
Michael Drogalis added a comment -

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?

Show
Michael Drogalis added a comment - 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?

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated: