Records feedback

Remi Forax forax at univ-mlv.fr
Thu Oct 1 21:24:12 UTC 2020


Hi David, 

> De: "Brian Goetz" <brian.goetz at oracle.com>
> À: "amber-spec-experts" <amber-spec-experts at openjdk.java.net>
> Envoyé: Jeudi 1 Octobre 2020 22:33:28
> Objet: Fwd: Records feedback

> Received on the -comments list.

A Record does not support encapsulation (separating the public API and the private implementation) given that it's a named tuple. 
If you want encapsulation, use a class. 

public class Nil<A> implements List<A> { 
private static final Nil<?> INSTANCE = new Nil<Object>(); 
private Nil() {} 
@SuppressWarnings("unchecked") 
public <A> Nil<A> nil() { return (Nil<A>) INSTANCE; } 
} 

As a side note, i've also fixed your use of @SuppressWarnings("rawtypes") which should be used mostly when dealing with code written before Java 5. 
Here, you should use @SuppressWarnings("unchecked") to workaround the fact that there is no type Nothing in Java and no way to express that Nil is covariant at declaration site. 

regards, 
Rémi 

> -------- Forwarded Message --------
> Subject: 	Records feedback
> Date: 	Thu, 1 Oct 2020 15:09:38 -0500
> From: 	David Aaron Kaye [ mailto:kayedavi at gmail.com | <kayedavi at gmail.com> ]

> To: 	[ mailto:java-se-spec-comments at openjdk.java.net |
> java-se-spec-comments at openjdk.java.net ]

> Good afternoon,

> I am very excited to see Records coming to Java. I have been playing around
> with them since JDK 14. I noticed that in the proposal, JDK-8246771
> [ https://bugs.openjdk.java.net/browse/JDK-8246771 |
> <https://bugs.openjdk.java.net/browse/JDK-8246771> ] , it says

> ...if the canonical constructor is explicitly declared then its access
> modifier must provide at least as much access as the record class

> I have implemented a functional List:

> public sealed interface List<A> permits Cons, Nil {
> }

> public record Cons<A>(A head, List<A> tail) implements List<A> {
> }

> @SuppressWarnings("rawtypes")
> public record Nil<Nothing>() implements List<Nothing> {
> public static final Nil instance = new Nil();

> private Nil() {
> }
> }

> I made the constructor private because I want this to be a singleton,
> accessed from instance, but the current proposal does not allow me to do
> that, and I get a compilation error on the private constructor. I could
> prefer to be able to make the constructor private so that I can have a
> record that is a singleton.

> Thanks,

> David Kaye
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20201001/5e72f083/attachment.htm>


More information about the amber-spec-experts mailing list