Summary
Pattern matching, a feature of languages such as Haskell, ML, and Scala, offers a convenient way to define sophisticated control flow. While Java does not provide pattern matching capabilities, the open-source Tom library adds pattern matching for Java. Luis Diego Fallas provides a brief pattern-matching overview for Java with Tom.
Advertisement
Tom is a superset of Java that adds pattern-matching capabilities to the language, mainly by providing a pattern matching compiler. Pattern matching is a more general application of the control flow structure provided by Java's switch statement, and is a core element of languages such as Scala or Haskell.
Adding pattern-matching to Java can yield more elegant and easy-to-understand programs in the face of complex logic, writes Luis Diego Fallas in a recent blog post, Pattern matching with Tom, devoted to the open-source library that adds pattern-matching capabilities to the Java language:
Tom provides a way for data structures that could be easily instantiated and manipulated with pattern matching. These data types are also called algebraic data types which are very similar to those available in languages such as Haskell...
These data types could be defined inside a Java class by using the %gom { } block or defining the types in a file with .gom extension.
Fallas provides an example of such a data structure:
module Company
imports int String
abstract syntax
Person = Person(companyId:int,name:String,age:int)
Persons = Persons(Person*)
Worker = Employee(personId:int)
| Contractor(name:String)
Workers = Workers(Worker*)
Group = DepartmentGroup(department:Department)
| Committee(name:String, members:Workers)
Department = SimpleDepartment(name:String, leader:Worker, members:Workers)
| MultiDepartment(name:String, leader:Worker, subDepartments:Departments)
Departments = Departments(Department*)
Groups = Groups(Group*)
Company = Company(name:String,members:Persons,groups:Groups)
Reading from the bottom to the top, these definitions describe a Company data structure which has name, a collection of persons and a collection of groups inside the company. Groups could be Departments or a Committees. Departments could be simple (SimpleDepartment) or have sub departments(MultipleDepartment). Also the members of the Committees or Departments could be employees or contractors...
In order to use these definitions we need to create a file with .t extension. This file will have both Java and Tom elements mixed together...
Using this simple example, Fallas provides a complete example of using Tom to perform pattern-matching on Java code:
Tom provides a powerful pattern matching mechanism that eases the manipulation of complex tree structures. In order to use this feature, a %match statement is provided. This statement is barely similar to a switch statement in Java or C# but using patterns instead of literals. A simple example of this feature is the following:
static void nameTest(Company c) {
String name = "";
%match (c){
Company(theName,_,_) -> { name = `theName; }
}
System.out.println(name);
}
In this example the name of the company is extracted and assigned to the name variable. The %match statement receives an element, in this case the instance of the Company datatype. This element is tested against one or more patterns, in this case only one pattern is provided. If the pattern matches, then the code on the left of the arrow ( -> ) is executed...
The rest of Fallas' post describes pattern matching on lists and non-linear patterns.
What do you think of Tom's attempt to add pattern matching to Java code? Do you believe the requirement to use a separate tool to obtain this feature is worth the benefits pattern matching provides?