Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Major
-
Resolution: Won't Fix
-
Affects Version/s: 1.0-JSR-4
-
Fix Version/s: None
-
Component/s: syntax
-
Labels:None
-
Environment:JSR4
-
Number of attachments :
Description
String s = "5"
int x = s
The effect of this code is that x is assigned the value of char '5'.
String s = "10"
int x = s
This produces a runtime exception.
If auto-conversion is absolutely needed between 1-character Strings and ints, then one would hope it would at least be limited to literal Strings. This behavior makes no sense.
this is no bug, this is the intended behavior. That is because groovy doesn't know characters, but handles characters as Strings of length 1. The ways I see to solve the problem above is to introduce chars or to forbid the handling of Strings of length 1 as chars when not explicitly converted to chars before.
String s = "5"
int x = s
would then throw an runtime exception.
String s = "5"
char c = s
int x = c
would not.
String s = "10"
int x = s
and String s = "10"
char c = s
would both cause an exception during runtime.
BTW, after the String is created, there is no way to know it was a liteal String.