[patterns] Pattern matching

Gavin Bierman gavin.bierman at oracle.com
Fri Jun 30 16:07:50 UTC 2017

Hi all,

The initial support for pattern matching, as proposed in JEP 305 [1],
has been pushed to the Amber repo. A longer discussion of this feature
can be found here [2].

What is currently supported?

We have added two new language constructs: the first to test an
expression against a pattern (`__matches`), and the second a statement
form (`__match`) that transfers control to one of several statements
depending on whether an expression matches one of several
patterns. The awkward keywords are to remind you that this is, for
now, an experimental language feature!

Currently, patterns are rather simple and given by the following

Pattern p ::= c         constant expression
              null      null patterns
              T x       type test with binding
              var x     variable binding

What restrictions are there?

* Currently we require all clauses in a __match statement to end with
  a break statement. In other words, there is no support for
  fall-through. The compiler will insert a break statement if it is
  not given explicitly.

* Currently we do not support parentheses in patterns.

* The compiler currently does not attempt any form of optimisation of
  pattern matching. We are currently prototyping a more efficient
  implementation [3]. 

* The treatment of type inference for `var` patterns is slightly
  different from that defined in JEP 286 [4]; in particular, no attempt is
  made to recover from infering non-denotable types. In the near
  future we intend to align these type inference processes.

* This is a prototype, so there are bugs. Please help us find and
  remove them!

What's coming next?

The most immediate enhancement planned is that we intend to merge the
__match statement with the existing switch statement. In other words,
the switch statement will be enhanced to support pattern matching. A
fuller discussion has been published [5]. 

We are planning a steady stream of enhancements to pattern
matching. Enhancements under consideration include the following:

* Guards
* An expression form of switch
* Nested patterns
* Supporting a continue statement in switch clauses to resume pattern
* Efficient runtime support for pattern matching


[1] http://openjdk.java.net/jeps/305
[2] http://cr.openjdk.java.net/~briangoetz/amber/pattern-match.html 
[3] http://cr.openjdk.java.net/~briangoetz/amber/pattern-match-translation.html
[4] http://openjdk.java.net/jeps/286
[5] http://cr.openjdk.java.net/~briangoetz/amber/switch-rehab.html

More information about the amber-dev mailing list