Non final records
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Thu Jan 25 13:26:09 UTC 2024
Hi Julian,
a record is just a wrapper around some data, which exposes nice
semantics properties - for instance, given a record Point(int x, int
y), it is always the case that:
```
Point p = new Point(1, 2);
assertEquals(new Point(p.x(), p.y()), p);
```
(this is actually the basis of why derived creation can be expressed).
In other words, records are meant to be stateless beasts (and for a
reason!). Of course you can still have array lists components in a
record, which can be mutated (added to), but I'd say using records this
way is non-idiomatic, can lead to suprising behavior (e.g. w.r.t.
pattern matching) and is discouraged.
A great writeup on why records are the way they are can be found here:
https://nipafx.dev/java-record-semantics/
Cheers
Maurizio
On 25/01/2024 13:18, Julian Waters wrote:
> Hi all,
>
> Just a quick question:
>
> Why isn't modifying the contents of a record supported formally?
> (Without reflection and the like) Wouldn't it be helpful to be able to
> modify a record's fields? Is there any plan to allow doing so in the
> future?
>
> best regards,
> Julian
More information about the amber-dev
mailing list