Wednesday, August 29, 2018

JDK Language Feature Preview in Action: Switch Expressions

JEP 12 ["Preview Language and VM Features"] is described on its main page as follows:

A preview language or VM feature is a new feature of the Java SE Platform that is fully specified, fully implemented, and yet impermanent. It is available in a JDK feature release to provoke developer feedback based on real world use; this may lead to it becoming permanent in a future Java SE Platform.

JEP 325 ["Switch Expressions (Preview)"] is, as the parenthetical in its title suggests, a "preview feature." As such, JEP 325 provides a high-visibility early opportunity to use the JEP 12-outlined process to preview a new language feature.

The recent messages on the OpenJDK jdk-dev mailing list provide evidence of the usefulness of JEP 325 in helping Java developers understand the implications of JEP 12 and the "preview feature" process. The discussion surrounding the "preview feature" nature of switch expressions (JEP 325) started with Mark Reinhold's August 27 message in which he wrote regarding JEP 325, "The few objections raised here are not new, having already been raised and answered over on the amber-dev and amber-spec-experts lists. I've therefore targeted this JEP to JDK 12." This message led to more feedback messages regarding objections to the currently proposed "switch expression" language implementation.

In response to some of the dialog (and specifically to a concern about not being able to change from the initial implementation once included in the JDK), Reinhold wrote in an August 28 message:

... the very point of a preview language feature is to invite further feedback without completely committing to the current design, so of course people will use it (we hope!). They're highly unlikely to use it in production, however, since preview features must be enabled explicitly, on the command line, at both compile time and run time. We're thus free to revise this design, based on new information, before it's etched into the stone of the language.

The flags currently proposed for explicitly enabling preview features for compile time and runtime are discussed in JEP 12. As currently spelled out there, compilation of preview features would require the presence of two flags passed on the command line to the javac compiler: --enable-preview and --release. The same document shows one flag needing to be passed to the java launcher at runtime to enable the preview feature: --enable-preview.

JEP 325 has already seen significant discussion on the mailing lists and I have highlighted a portion of this discussion in previous blog posts "Switch Expressions Coming to Java?" and "Enhancing Java switch Statement with Introduction of switch Expression." However, the discussion has been largely held on the amber-dev and amber-spec-experts mailing lists, so Brian Goetz has provided a summary of the previous discussion points and reasons for decisions made related to the "switch expressions" preview language feature. Goetz concluded his summary with this statement regarding the preview nature of switch expressions.

The preview mechanism will allow us to gather feedback on the feature from actual use, rather than theorizing from no examples, and potentially adjust the specification before final release if warranted. So if any _new_ issues up come as a result of actual experience, we are happy to hear about them.

In a separate (later) message, Goetz provided more background details related to the "preview feature" concept. He wrote:

A Preview language feature still needs to be Done, to the same level of Done, as a permanent feature. The difference is that we have a (short) "grace period" where we have a chance to correct serious errors that have leaked past the usual process, without having to pay the Incompatibility Penalty. With our new cadence, I expect that most non-trivial language features will go through the Preview mechanism going forward. This does not mean they are experimental, or of lower quality, or have had less thought put into them. But, it does sometimes happen that we discover unexpected interactions only after things have been tried by a broader audience, and for this, Preview gives us a short window to correct such issues if they are found early enough.

Switch Expressions (JEP 325) provides an opportunity to try out the "preview language feature" concept outlined in JEP 12. It's a feature that many people have interest in and many people have opinions on, which makes it even more interesting as a feature to take JEP 12 out for a test spin. As developers play with the preview implementation of JEP 325 now targeted for JDK 12, it'll be interesting to see how real use of the feature strengthens and weakens the arguments for the current implementation.

No comments: