Saturday, November 12, 2022

Java 19: Record Patterns

Java 19 introduces Record Patterns, a preview language feature, which allow us to "deconstruct" records and access their components directly.

Here is an example:

record Book(String title, String author, double price) {}

static void print(Book b) {
  if (b instanceof Book(String title, String author, double price)) {
    System.out.printf("%s by %s for %.2f", title, author, price);
  }
}

Book(String title, String author, double price) is a record pattern that decomposes an instance of a Book record into its components: title, author and price.

null does not match any record pattern.

Record patterns can be used in switch statements and expressions as well:

static double getPrice(Publication p) {
  return switch(p) {
    case Book(var title, var author, var price) -> price;
    case Magazine(var title, var publisher, var price) -> price;
    default -> throw new IllegalArgumentException("Invalid publication: " + p);
  };
}

Note that I have used var inside the record patterns, allowing the compiler to infer the type of each component, thus saving me from having to explicitly state it.

Record patterns can also be nested inside one another, in order to decompose complicated object graphs. For example:

record Author(String firstName, String lastName) {}
record Book(String title, Author author, double price) {}

static void print(Book b) {
  if (b instanceof Book(var title, Author(var firstName, var lastName), var price)) {
    System.out.printf("%s by %s %s for %.2f", title, firstName, lastName, price);
  }
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.