Submission: switch (...) instanceof feature

brucechapman at paradise.net.nz brucechapman at paradise.net.nz
Sun Mar 29 15:43:23 PDT 2009


Quoting Jeroen van Maanen <jeroen at entreact.com>:

> I'd like to coin a switch (...) instanceof statement as a new feature of
> the Java language. Please accept the attached proposal for review.
> 
> Regards, Jeroen
>  

+1 for this idea. I had thought about writing up something similar but have run
out of time.

At this stage I'd call your proposal a draft, there is a lot of work to bring it
up to the required standard IMHO.

A/ Specifically you syntax sugaring is deficient in a number of areas.

1/ there is no enclosing switch, while, do or for around the statemnt, therefor
the meaning of any "break" statement in the original switch either changes or
becomes invalid. A nasty hack would to to wrap a "do {} while(true);" around the
desugaring.

2/ Using "else if" means drop through doesn't work. Drop through in this case is
problematic unless <<casetype(n+1)>> is a supertype of <<casetype(n)>>. You need
to specify what happens if there is no break.

3/ Your synthetic variable gives surprising results if <<identifier>> appears on
the LHS of an assignment operator, because you will assign to the synthetic
variable, not the obvious one. Solutions are to only allow identifiers declared
"final" (and make the synthetic variable fine) OR don't use a synthetic variable
and instead insert the case everywhere the identifier is used (except on LHS of
assignment).

It might be better to drop the desugaring from the definition and just explain
the semantics.

B/ I think it is essential that the meaning of identifier has the case's type
inside the statements - you are doing this.

C/ The syntax I was thinking of was more like

switch(<<primary>> instanceof ?) {...

which is a little less surprising than yours, since all that is changing is what
is inside the parenthesis, not adding something between the parenthesis and braces.

D/ For what it is worth, I think we can save 50% of the pain of existing use
cases using an API and closures. I did a jug talk on it back in June 2007 See
http://jug.wellington.net.nz/presentations/Java_Tidbits.pdf#page=37 for example
s of my dream syntax, and how to do it with closures.  You might find parts of
that presentation useful for the major benefit / major advantage sections. Show
people the pain and the amount of "repeating yourself" with the current code.

E/ You may well need to address the definite assignment rules since they get
tricky around switch statements IIRC.


F/ If we are doing String switches in coin, then instanceof seems just as
valuable (indeed more valuable when you look at how you need to do it today).

Regards

Bruce






More information about the coin-dev mailing list