JDK-6978894 : Allow handling of object in switch statement using new Switchable interface.
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 6u21
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_7
  • CPU: x86
  • Submitted: 2010-08-20
  • Updated: 2012-07-24
  • Resolved: 2012-07-24
Related Reports
Duplicate :  
Relates :  
Description
A DESCRIPTION OF THE REQUEST :
Allow any object to implement "Switchable," exposing particular required functions necessary for the compiler to make calls and switch against the object.

Better yet, take this process one step further and allow for extensible auto-boxing using an implementing Interface to define auto-boxing behavior.

JUSTIFICATION :
Handling of switchable objects is too "special case" with handling of very specific object types in an inflexible manner.  This runs contrary to the object-oriented and extensible nature of Java.

By providing a "Switchable" interface, any object can be enhanced for utilization within a switch statement.  Change Number and Enum to implement/extend this interface as necessary.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Have any object be able to implement "Switchable" interface, which forces implementation of a function which returns an integer-compabitible type necessary for switching.  Any "Switchable" should be capable of being utilized within a switch statement.

---------- BEGIN SOURCE ----------
Handling of switchable objects is "special case" with handling of very specific primitives and object types in an inflexible manner.  The behavior is "hard-coded" and inflexible.
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
No known workaround which can still utilize switch statements.

Comments
EVALUATION Java SE 7 supports switching on strings. Use the visitor pattern on a class to support coding alternatives. Closing as will not fix.
23-07-2012