Records and bi-directional relationships

Brian Goetz brian.goetz at oracle.com
Sat Dec 14 22:29:48 UTC 2019


>     And the answer is, mostly no.  (You can have records that have
>     mutable
>     components and then later mutate them to be cyclic.
>
>
> That's interesting, how would I declare a mutable component? From 
> reading the JEP I concluded they'd be final.

The fields themselves are final, but they can refer to mutable objects:

     record Foo(Object[] a) { }
     record Bar(Foo f) { }

     Foo f = new Foo(new Object[1]);
     Bar b = new Bar(f);
     f.a()[0] = b;  // don't code like this

This is what I meant, but I wasn't really recommending it.  Records are 
_shallowly immutable_ (because, 1995.)

> In that light, isn't the potential SOE in toString() even more of a 
> concern than in the very specific corner case of an escaping reference 
> I sketched?

Yes, that's awful too :)


More information about the amber-dev mailing list