Limited pattern matching for Java

Project Coin on OpenJDK is soliciting ideas for small language changes for Java 7. The ideas so far appear to suggest features from C#, which is a superior language IMHO. In principle, I hate the idea of baking in cosmetic changes to a language. Scheme and Lisp macros are a profoundly better solution to syntactic issues, but mainstream programmers will never grok it. So Java has no choice but to add more stuff. Therefore, I think the switch statement is seriously underutilized in C-like languages. One idea is to add support for a limited degree of object pattern matching. Think of it as a painfully limited version of the case expression in many functional languages.

The switch in Java currently only allows a few primitive types. Change it so now you can have switch(objectType). Allow the case arms to support a boolean expression. To simplify things, allow access to object fields like this: “.field”. So the new switch would look like this:

   1: switch (myCustomer) {
   2:     case (.name == "Bob"): stmts ;
   3:     case (.age > 21 && .income > 20000): stmts ;
   4:     default: stmts ;
   5: }

A simpler idea is to stick with the regular switch statement, but support multiple switch variables. Right now, you can only do “switch (oneItem)”. Instead, you could allow “switch (oneItem, twoItem)” and more. In the case arms, right now they allow “case constant:”. Instead, this can be extended to allow “case constant1, constant2” and more. Obviously, each constant is matched with the switch variable in the same position. If there are too many constants, that’s a syntax error. If there are fewer constants, then ignore the remaining variables. Here’s an example:

   1: switch (document, output) {
   2:     case "pdf", "printer": print it out;
   3:     case "pdf", "screen": use acrobat;
   4:     case "jpg", "printer": turn on fancy colors and print;
   5:     default: b/w print;
   6: }

Finally, the absolute simplest extension to the existing switch statement is to allow case ranges. This is already implemented in Visual Basic.NET with their Select Case statement. You say “Case 1 to 10” to match that range. Lots of languages support this. This feature won’t change the world, but it does make switch a bit more useful.

Advertisements

2 comments

  1. Sun Yi

    Awesome stuff!

    I know there have been lots of recommended changes for the JDK7, but very few of those will eventually make it. In fact, some of those features that are not likely to make it into JDK7 will be implemented in to the FAN Language.

    Luckily for us, Stephen Colebourne and co. will be giving a talk on FAN on the 10th of March. Click on my name to learn more.

  2. Russell

    Interesting proposal – I certainly like it. Too bad you probably can’t have a more complete pattern matching/unification solution but I don’t see how you could twist the syntax of Java to accommodate it.

    Your remark that macros are probably a better solution seems really true (except for the whole parsing java statements question). Are there any viable proposals floating out there for macros? Something beyond the C string-based approach?

    I have mixed feelings about the .NET approach of edging into the arena with Expression. On the one hand, traversing the expression tree seems very cumbersome and awkward, even compared with scheme’s hygienic macros. On the other hand, that complexity does rather ensure that there won’t be frivolous macros created!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s