[patterns] Pattern matching
gavin.bierman at oracle.com
Fri Jun 30 16:07:50 UTC 2017
The initial support for pattern matching, as proposed in JEP 305 ,
has been pushed to the Amber repo. A longer discussion of this feature
can be found here .
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
* The treatment of type inference for `var` patterns is slightly
different from that defined in JEP 286 ; 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
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 .
We are planning a steady stream of enhancements to pattern
matching. Enhancements under consideration include the following:
* An expression form of switch
* Nested patterns
* Supporting a continue statement in switch clauses to resume pattern
* Efficient runtime support for pattern matching
More information about the amber-dev