Deconstructor return type as record ?

Brian Goetz brian.goetz at oracle.com
Thu Jan 2 21:22:44 UTC 2020


There are at least two questions here, one having to do with records, 
the other having to do with pattern matching.

Recall, in Lambda, how we chose to do nominal function types (functional 
interfaces) rather than structural ones.  Records make a similar choice 
-- rather than doing structural _tuples_, we choose to do nominal ones.  
So the question you are raising becomes: is there an equivalent of "SAM 
conversion" for records, as there was for lambdas?  In your example, you 
posit one example: that an expression of the syntactic form (x,y) can be 
converted to a record of the right shape.

I think the answer here is "we're still not sure".  It's been an idea 
that's been sitting around in the idea pile for a while, but so far I've 
not been thrilled with some of the ways we might have exposed it.  Ideas 
like this are superficially attractive, but they don't go very deep; 
they only optimize the instantiation.  And, while you could say the same 
for lambdas vs inner classes, in the tuple-vs-constructor example, the 
difference is much smaller than it was for lambdas vs inner classes 
(both syntactically and semantically.)  So I think this is indeed sugar, 
and the sugar goes on last.

As to the pattern matching aspect, I know you've been wanting to have 
the "what does a deconstructor look like" discussion for a long time, 
but we're still not ready to have that discussion.  I promise, these 
requests have not been forgotten, and we'll bring it up when we're ready 
to!

On 1/2/2020 4:08 PM, Remi Forax wrote:
> We have introduce records because it's a simple abstraction to extract values when doing the pattern matching.
> Given that records are named tuples, does it means that a deconstructor is just a classical method with no parameter and a record as return type ?
>
> public class Point {
>    private final double rho, theta;
>
>    public Point(double x, double y) {
>      rho = Math.hypot(x, y);
>      teta = Math.atan(y / x);
>    }
>
>    public record RectangularPoint(double x, double y) { }
>    public RectangularPoint deconstructor() {
>      return new RectangularPoint(rho * Math.cos(theta), rho * sin(theta));
>    }
> }
>
> and with a little syntactic sugar to see any tuples as a Record (like we see any lambdas as a functional interface)
>    public RectangularPoint deconstructor() {
>      return (rho * Math.cos(theta), rho * sin(theta));
>    }
>
>
> Rémi

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20200102/e994f519/attachment.htm>


More information about the amber-spec-experts mailing list