Bug in switch around Enums?
Jonathan Gibbons
Jonathan.Gibbons at Sun.COM
Sat Sep 29 13:19:01 PDT 2007
Hey guys,
Just a gentle reminder this alias is for developers working on the
compiler, and is not intended to be a general forum for discussing
the shortcomings of and enhancements to the language itself.
-- Jon
On Sep 29, 2007, at 11:42 AM, Brian Slesinsky wrote:
> Sure, support for constant expressions that return an enum seems
> like a win.
>
> It would also solve a different gotcha. This doesn't work:
>
> void doSwitch(MyEnum x) {
> switch (x) {
> case MyEnum.FOO:
> // do something
> }
> }
>
> This was the first thing I tried (by experiment, rather than reading
> the spec) and for a short time I was under the impression that enums
> don't work with switch statements. The same thing happened to one
> other person I know. Even if the spec isn't changed, clear error
> messages help. (Although I believe it was IntelliJ that confused me,
> not javac.)
>
> - Brian
>
> On 9/29/07, Tom Ball <Tom.Ball at sun.com> wrote:
>> Okay, it's not a bug since the spec says its not. It is still an
>> unnecessary syntactic inconsistency between constants and enums,
>> however, and inconsistencies like this making the language a little
>> harder to learn (yet another rule to remember). This can't be
>> changed
>> in the compiler until the spec is, but perhaps there is a related
>> pending JSR which can add it as an update.
>>
>> Tom
>>
>> Matthias Ernst wrote:
>>> Ted,
>>>
>>> according to the JLS it's not a bug.
>>>
>>> Switch labels can either be constant expressions or enum
>>> identifiers:
>>>
>>> http://java.sun.com/docs/books/jls/third_edition/html/
>>> statements.html#14.11
>>> SwitchLabel:
>>> case ConstantExpression :
>>> case EnumConstantName :
>>> default :
>>>
>>> and constant expression is only defined for primitives and String:
>>> http://java.sun.com/docs/books/jls/third_edition/html/
>>> expressions.html#15.28
>>>
>>> So (STAGGER) is neither a constant expression nor an enum
>>> constant name.
>>>
>>> Feel free to call me type 1
>>> (http://diveintomark.org/archives/2004/08/16/specs) :-)
>>>
>>> Matthias
>>
>>
More information about the compiler-dev
mailing list