Multiple return values

Lukas Eder lukas.eder at gmail.com
Fri Jan 11 15:57:19 UTC 2019


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


More information about the amber-spec-comments mailing list