How will record destructing be implementable without multiple return values?
Steven Stewart-Gallus
sstewartgallus at sstewartgallus.com
Sat Apr 27 00:26:21 UTC 2019
Hello,
It seems like there are potential plans for record destructuring but
not fully hashed out yet in a formal JEP
https://cr.openjdk.java.net/~briangoetz/amber/pattern-match.html? The
formal JEPs https://openjdk.java.net/jeps/305 seem to be much more
scaled back.
Record destructuring doesn't seem implementable efficiently to me on
the JVM as shown. I guess this is why the JEP was scaled back?
Suppose we have a Point class and we want to destruct in radians
format.
switch (obj) {
case Point.ofRadians(var theta, var radius):
return doStuff(theta, radius);
}
How can you return multiple values to make this work without
allocating? Or is this just planned to be put on the back burner
until things like Project Valhalla go through which would make this
much less of an issue. I think an encoding like:
public static Synthetic ofRadians(Object obj) {
if (obj instanceof Point p) {
return new Synthetic(true, p.theta(), p.radius());
}
return new Synthetic(false, 0, 0);
}
public inline record Synthetic(int matched, int theta, int radius) {
}
might work but it seems silly.
Or am I missing something and this isn't really a problem.
Thank you,
Steven Stewart-Gallus
More information about the amber-spec-comments
mailing list