Package Private Record Fields

August Nagro augustnagro at gmail.com
Tue Apr 28 20:37:11 UTC 2020


I'm enjoying using the Records preview so far.

One thing I've noticed is that classes in the same package as the Record
are likely to be heavy users of its public accessor methods, and that the
`()` can inhibit readability. For example,

org/example/Point.java
record Point(double x, double y) {}

org/example/MathFunctions.java:
import static java.lang.Math.*;

class MathFunctions {
  public static double distance(Point a, Point b) {
    return sqrt(pow(b.x() - a.x(), 2) + pow(b.y() - a.x(), 2));
  }
}

It would be nice to be able to write:

class MathFunctions {
  public static double distance(Point a, Point b) {
    return sqrt(pow(b.x - a.x, 2) + pow(b.y - a.x, 2));
  }
}

I understand the reason for not making them public.. the accessor methods
are good API design. But giving Record fields the default modifier wouldn't
change this.

Regards,

August


More information about the amber-dev mailing list