Package Private Record Fields

August Nagro augustnagro at gmail.com
Wed Apr 29 14:43:57 UTC 2020


Thanks, I misunderstood your earlier mail.

I have already been declaring some records in the class that makes the most
sense, but I'd argue that this is a poor substitute for package private
fields.

I hope it doesn't seem too much of a bikeshed over two `()` characters, but
I think it does make a difference, especially when using records for math.

On Wed, Apr 29, 2020, 9:03 AM Mateusz Romanowski <
romanowski.mateusz at gmail.com> wrote:

> OK, here you go.
>
> You can put the static method directly on the record, which is better
> style as far as I am concerned:
> ---
> package ex;
> import static java.lang.Math.*;
> public final record Point(double x, double y) {
>   public static double distance(Point a, Point b) {
>     return sqrt(pow(b.x - a.x, 2) + pow(b.y - a.y, 2));
>   }
> }
> ---
>
> Or, you can put record inside the utility class:
> ---
> package ex;
>
> import static java.lang.Math.*;
>
> public class Maths {
>   record Point(int x, int y) { }
>
>   public static double distance(Point a, Point b) {
>     return sqrt(pow(b.x - a.x, 2) + pow(b.y - a.y, 2));
>   }
> }
> ---
>
> /Mateusz
>
>
> On Tue, Apr 28, 2020 at 8:51 PM August Nagro <augustnagro at gmail.com>
> wrote:
>
>> Hi Mateusz,
>>
>> How would that improve the readability? Can you share an example?
>>
>> On Tue, Apr 28, 2020 at 3:45 PM Mateusz Romanowski <
>> romanowski.mateusz at gmail.com> wrote:
>>
>>> Hi August,
>>> Have you considered putting "friend" methods in the same source file,
>>> and therefore using nest-based access control?
>>>
>>> Cheers,
>>> Mateusz
>>>
>>> On Tuesday, April 28, 2020, August Nagro <augustnagro at gmail.com> wrote:
>>> > 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