Hyphenated keywords and switch expressions
Ben Evans
benjamin.john.evans at gmail.com
Fri Jan 11 10:19:25 UTC 2019
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-comments
mailing list