JDK-4032356 : Java needs a typecase-like statement
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 1.1,6
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: generic,solaris_2.5.1,windows_nt
  • CPU: generic,x86,sparc
  • Submitted: 1997-02-13
  • Updated: 1997-12-17
  • Resolved: 1997-12-17
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Description

Name: mc57594			Date: 02/13/97


I often find myself writing code that looks like this:

void blah (Object x)
{
    if (x instanceof ClassOne)
    {
        ClassOne x1 = (ClassOne) x;
        ...;
    }
    else if (x instanceof ClassTwo)
    {
        ClassTwo x2 = (ClassTwo) x;
        ...;
    }
    ...;
}

It'd sure be nice if Java offered a typecase-like statement,
especially since it already has it for exceptions! The above could
look something like this:

void blah (Object x)
{
    typeswitch (x)
    {
        case ClassOne x1:
        {
            ...;
            break;
        }
        case ClassTwo x2:
        {
            ...;
            break;
        }
        ...;
    }
}

company  -  Electric Communities  , email  -  ###@###.###
======================================================================

Comments
WORK AROUND Name: mc57594 Date: 02/13/97 Use if-else if-etc. with "instanceof" ======================================================================
11-06-2004

PUBLIC COMMENTS The convenience of a typecase statement does not justify a language change.
10-06-2004

EVALUATION Java does not need more statements; if anything, it needs less. gilad.bracha@eng 1997-12-17
17-12-1997