Fwd: Multiple return values

Brian Goetz brian.goetz at oracle.com
Fri Jan 11 15:58:51 UTC 2019


Received on the comments list.

> Begin forwarded message:
> 
> From: Lukas Eder <lukas.eder at gmail.com>
> Subject: Multiple return values
> Date: January 11, 2019 at 10:57:19 AM EST
> To: amber-spec-comments at openjdk.java.net
> 
> Hello,
> 
> I'm referring to the exciting proposed new features around destructuring
> values from records and other types as shown here:
> https://cr.openjdk.java.net/~briangoetz/amber/pattern-match.html#pattern-bind-statements
> 
> The example given was:
> 
>  Rect r = ...
>  __let Rect(var p0, var p1) = r;
>  // use p0, p1
> 
> This is a very useful construct, which I have liked using in other
> languages a lot. Just today, I had a similar use case where I would have
> liked to be able to do something like this, but without declaring a nominal
> type Rect. Every now and then, I would like to return more than one value
> from a method. For example:
> 
>  private X, Y method() {
>    X x = ...
>    Y y = ...
>    return x, y;
>  }
> 
> I would then call this method as follows (hypothetical syntax. Many other
> syntaxes are possible, e.g. syntaxes that make expressions look like
> tuples, or actual tuples of course):
> 
>  X x, Y y = method();
> 
> The rationale is that I don't (always) want to:
> 
> - Modify either type X or Y, because this is just one little method where I
> want to indicate to the call site of method() in what context they should
> interpret X by providing a context Y
> - Wrap X and Y in a new type, because creating new types is too much work
> - Wrap X and Y in Object[] because that's just dirty
> - Rely on escape analysis for some wrapper type (minor requirement for me)
> - Assign both X and Y. Something like "X x, _ = method()" or "_, Y y =
> method()" would be useful, too.
> 
> I was wondering if in the context of all the work going on in Amber around
> capturing local variables, etc. if something like this is reasonably
> possible as well in some future Java.
> 
> This is possible in Python. Go uses this syntax to return exceptions.
> 
> Thanks,
> Lukas

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20190111/c88222c0/attachment-0001.html>


More information about the amber-spec-experts mailing list