Records and bi-directional relationships

Brian Goetz brian.goetz at oracle.com
Sat Dec 14 22:11:56 UTC 2019


I think you are asking: is it possible to create cycles with records?  
And the answer is, mostly no.  (You can have records that have mutable 
components and then later mutate them to be cyclic.

What you're doing in the `Surface` constructor is allowing the `this` 
reference escape during construction, which undermines the memory model 
guarantees for final fields.

On 12/14/2019 4:58 PM, Gunnar Morling wrote:
> Hi all,
>
> I was exploring the records feature in the latest 14 EA build a bit. An
> interesting question came up in how records will work for
> bi-directional relationships between two record types.
>
> I found no way to instantiate such pair of records without creating a
> temporary instance of one of the two (unlike with regular classes, where I
> could wire the relationship from within the constructor of one). Did I miss
> an alternative way to do so (see below for what I came up with)?
>
> A related question: this program will raise a StackOverflowError in the
> toString() method. Is this an accepted limitation?
>
> Thanks,
>
> --Gunnar
>
>      public class RecordsBidi {
>          public static void main (String... args) {
>              Surface surface = new Surface(new Point(1, 2));
>              System.out.println(surface);
>          }
>
>          record Point(int x, int y, Surface owning) {
>              public Point(int x, int y) {
>                  this(x, y, null);
>              }
>          }
>
>          record Surface(Point center) {
>              public Surface(Point center) {
>                 this.center = new Point(center.x, center.y, this);
>              }
>          }
>      }



More information about the amber-dev mailing list