Bad iteraction between the compact constructor and parameters captured by a lambda
Remi Forax
forax at univ-mlv.fr
Sat Oct 24 20:48:44 UTC 2020
Oops, instead of
var copy = new int[items.length];
it should be
var copy = new int[items.length][];
I've fixed the codes below.
----- Mail original -----
> De: "Remi Forax" <forax at univ-mlv.fr>
> À: "amber-spec-experts" <amber-spec-experts at openjdk.java.net>
> Envoyé: Samedi 24 Octobre 2020 22:45:15
> Objet: Bad iteraction between the compact constructor and parameters captured by a lambda
> When we have decided to ban
> this.items = items
> in the compact constructor and ask our users to use an assignment instead,
> it has a stupid side effect to make the parameters inside the compact
> constructor not effectively final, so a lambda can not capture them.
>
> So a code like this doesn't compile :(
>
> record Matrix(int[][] items) {
> Matrix {
> var copy = new int[items.length][];
> Arrays.setAll(copy, i -> items[i].clone());
> items = copy; // items is not effectively final anymore
> }
> }
>
>
>
> Obviouly, introducing an intermediary local variable works, but it's not pretty
>
> record Matrix(int[][] items) {
> Matrix {
> var copy = new int[items.length][];
> var items2 = items;
> Arrays.setAll(copy, i -> items2[i].clone());
> items = copy;
> }
> }
>
>
> I'm not sure what we should do here :(
> I kind a like the current rules of a compact constructor.
>
> Rémi
More information about the amber-spec-experts
mailing list