EVALUATION
As I noted in 6558543, boxing should be orthogonal to otherwise-legal widening/narrowing. I see no reason not to allow up-conversion from short, byte, etc to Integer, Long, et al. JLS 5.2 should allow: "a widening primitive conversion (5.1.2), ***optionally followed by a boxing conversion***". The switch code in 6578574 would then compile.
|
EVALUATION
Here's what the JLS have to say about it:
The type of the [switch] Expression must be char, byte, short, int, Character, Byte, Short, Integer, or an enum type (??8.9), or a compile-time error occurs.
[...]
All of the following must be true, or a compile-time error will result:
Every case constant expression associated with a switch statement must be assignable (??5.2) to the type of the switch Expression.
In this case we have that the switch variable has type 'Integer', which is legal. Then we have that the compiler generates an error when a case statement, whose constant is of type 'short' is added. The problem is that 'short' is not assignable - literally speaking' to Integer, as the only moves allowed by JLS 3rd 5.2 are:
*) an identity conversion (??5.1.1)
*) a widening primitive conversion (??5.1.2)
*) a widening reference conversion (??5.1.5)
*) a boxing conversion (??5.1.7) optionally followed by a widening reference conversion
*) an unboxing conversion (??5.1.8) optionally followed by a widening primitive conversion.
So, if we apply boxing to 'short' we end up with Short which cannot then be converted to Integer by widening reference conversion. REassigning to spec for further evaluation.
|