Hyphenated keywords and switch expressions

Tagir Valeev amaembo at gmail.com
Fri Jan 11 13:32:04 UTC 2019


To my personal taste making longer keyword for switch expression is
bad. Expressions should be more compact compared to statements, and
switch expression is already quite verbose (e.g. compared to ?: which
is close to two-case switch expression). So having a separate keyword
like `switch-expr` would only make the things worse.

On the other hand, from IDE developer point of view, having expression
and statement with so similar syntax definitely adds a confusion to
the parsing (and probably to users). E.g. suppose we want to parse a
fragment which consists of a number of statements, isolated from other

switch(0) { default -> throw new Exception(); };

In normal context it's two statements: switch-statement followed by an
empty statement. However inside switch expression rule it's one
statement: an expression statement containing a switch expression:

int x = switch(0) { default -> switch(0) { default -> throw new
Exception(); }; };

Normally if we take a textual representation of single statement, it
could be parsed back into the same single statement, when isolated
from other code (the same works for expressions). Here this rule is
violated: the expression statement taken from switch expression rule
could be reparsed in isolation as two statements. This poses a problem
for us as we not only parse complete program, but often perform code
transformations during refactorings/quick-fixes which assumes code
fragments joggling / reparsing. Surely we can do something with this,
but this is ugly. Of course separate keywords would solve the problem.

Another a little bit confusing thing is lambdas. E.g.:
IntSupplier s = () -> switch(0) { default -> throw new Exception(); };
-- this code compiles
Runnable s = () -> switch(0) { default -> throw new Exception(); }; --
this doesn't, need to wrap switch into braces
One may think that the former is a switch statement, but actually it's
an expression. It would be more clear were we had different keywords.

So there are ambiguities and confusing cases. Still I think they are
bearable and we can live with them. More interesting question is
whether we can change `break` keyword on the switch expressions. As
it's rarely necessary, to my opinion it's perfectly ok to make it
longer. Disambiguating label-break and value-break in IDE code is also
somewhat ugly. Again as we may joggle with statements we cannot
interpret whether it's label or value without proper context and
sometimes the meaning could change inadvertently.

These are just some thoughts to add to the overall picture. In general
we're still fine with current spec.

With best regards,
Tagir Valeev.

On Fri, Jan 11, 2019 at 7:48 PM Brian Goetz <brian.goetz at oracle.com> wrote:
> Received from the -comments list.
> Begin forwarded message:
> From: Ben Evans <benjamin.john.evans at gmail.com>
> Subject: Hyphenated keywords and switch expressions
> Date: January 11, 2019 at 5:19:25 AM EST
> To: amber-spec-comments at openjdk.java.net
> Hi EG members,
> I had a couple of comments on hyphenated keywords.
> First off, I think they're a great idea, if used judiciously. One of
> Java's great strengths when teaching it to beginners is the simplicity
> and explicit regularity of the grammar. A few more keywords, keeping
> clear the distinction between keywords and other language constructs,
> is IMO a good thing - especially when it allows us to tidy up and
> explicitly express language ideas that we currently can't.
> For example, the lack of package-private as an explicit keyword is one
> of the most common sources of confusion and errors I see in new Java
> devs - especially those who already know other programming languages.
> If we are going to do hyphenated
> Secondly, a question. Is it worth reconsidering the choice of keyword
> used for switch expressions with hyphenation in mind? I re-read the
> discussion about not wanting to see switch statements abandoned, and a
> lack of consistency by using a new match keyword (imposed by eminent
> domain), and I can see the validity of a lot of the points made.
> However, with hyphenated keywords, we have other possibilities - so
> what about using something like switch-expr (or switch-expression)
> instead? With the rules:
> 1. switch is only legal in statement form (the current Java 11 behaviour)
> 2. switch-expr is only legal for the expression form
> To my mind, this helps in a few ways:
> a) It maintains the cognitive connection between switch expressions
> and switch statements, and doesn't lead to the feeling that switch
> statements are abandonware
> b) It provides a clear clue to newcomers about the distinction between
> the two forms (bear in mind that beginners often don't develop a full
> grasp of the distinction between statements and expressions until they
> have gained some proficiency with the language. The differentiation of
> the keyword could help provide visual clues and avoid
> hard-to-understand-if-you're-a-newbie compiler errors when debugging)
> c) There is no cognitive overhead for experienced programmers.
> d) IDEs will easily be able to autodetect and offer to correct if the
> wrong keyword is used "Did you mean switch-expr instead of switch?"
> I'd be really interested to hear what people think - the above is very
> much with my "teaching newbies" hat on, and I know that's far from the
> only concern here.
> Cheers,
> Ben

More information about the amber-spec-observers mailing list