Submission: switch (...) instanceof feature

Ulf Zibis Ulf.Zibis at gmx.de
Fri May 1 04:05:03 PDT 2009


Am 01.05.2009 09:09, Derek Foster schrieb:
> I notice that the example you gave is actually an argument for an "if" that respects instanceOf, rather than a 'switch' that does. It seems to me like an abuse of notation to use a 'switch' in a case like this where only one decision (it is or it isn't a subtype of a given type) is being made.
>   

Yes, in this simple example you are potentially right, if you ignore the 
3rd case, when Object o is nor String nor Charset type AND accept the 
casting in the "else branch". But keep in mind, that there potentially 
could be more types to switch from.
In other words, please provide code sample using 'if instanceof' syntax 
which would fulfil the needs of given method "Charset lookup(String 
lowCanonical)", which completely omits casting.

> Although I am skeptical of the 'switch instanceof' feature, for the reasons I outlined below,

Where ???

-Ulf


>  I would very much like to see Java implement the 'if instanceof' feature that is implied in the proposal, and which various people have been suggesting, so that this very common coding pattern:
>
> if (foo instanceof Bar) {
>      Bar bar = (Bar)foo;
>      doSomethingWith(bar);
> }
>
> could be simplified to:
>
> if (foo instanceof Bar) {
>      doSomethingWith(foo);
> }
>
> with no loss of type safety. It seems to me that your example would be more readable with such a construct than it is with the 'switch instanceof' feature.
>
> I had intended to submit a proposal along these lines myself, but simply ran out of time to put it together. Still, it seems to me that there's been widespread agreement on the list that the 'if instanceof' sub-proposal is a good idea.
>
> Derek
>
>
>   




More information about the coin-dev mailing list