Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

TLDR

* Destructuring via Record Patterns

The most prominent feature is the ability to use a record pattern on the left-hand side of a local variable declaration. This allows you to "destructure" an object and initialize multiple variables in a single statement.

Traditional way:

  Point p = getPoint();
  int x = p.x();
  int y = p.y();
Enhanced way:

  Point(int x, int y) = getPoint();
This also supports nested patterns, allowing you to reach deep into an object hierarchy in one go:

  Circle(Point(int x, int y), double radius) = getCircle();
* Pattern Matching in Enhanced for Loops

You can now use these same record patterns in the header of an enhanced for loop to extract data from every element in a collection or array.

  for (Circle(Point(int x, int y), double radius) : circles) {
      // Directly use x, y, and radius here
  }
 help



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: