[2449] in java-interest

home help back first fref pref prev next nref lref last post

Re: switch is too limiting

daemon@ATHENA.MIT.EDU (Arthur van Hoff)
Mon Oct 2 22:20:25 1995

Date: Mon, 2 Oct 1995 10:07:30 -0700
From: Arthur.Vanhoff@Eng.Sun.COM (Arthur van Hoff)
To: lemayp@lne.com
Cc: java-interest@java.Eng.Sun.COM


Hi Laura,

> So I've discovered, through the time-honored debugging method known as the
> compiler error, that switch statements only work if the test and the constant
> are both ints or can be cast to ints (i.e. numerics or chars).
> 
> sigh.
> 
> I would have hoped that switch in java would be more flexible than
> that, and that you could switch with strings or other objects.  Yes,
> I know that switch would only work if == was meaningful for object
> values, and that gets into the whole operator overloading discussion,
> so I won't start that.
> 
> But it does seem awfully limiting to me.  I would love to be able to
> use strings as identifiers, for example, and to be able to do something
> like this:
> 
> switch (alignment) {
>     case "left" : {
>       // code to align stuff left
>     }
>     case "right" : {
>       // code to align stuff right
>     }
>     case "center" : {
>       // code to align stuff center
>     }
> }
> 
> Instead, I either have to use a whole lot of nested ifs (in which I can
> do alignment.equals("right")), or set up integer constants to represent
> everything I wanted to be a string in the first place.
> 
> sigh.
> 
> Maybe in 2.0?

I've always wanted that feature too. I'll see if we can get it
added in Java 2.0.

Have fun,

	Arthur van Hoff
	
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post