Tuples Was: Small things Java lets you do, but not everywhere.

forax at univ-mlv.fr forax at univ-mlv.fr
Tue Aug 21 14:19:31 UTC 2018


ok, got it.

The current plan is to introduce tuples once we have value type, because unlike arrays, a value type can be stack allocated or even register allocated.

This remember me an interresting question, should a tuple always be be named in Java ?
Tuples are like function types, there are structural types, not nominal types.

In Java, to keep function type nominal, we invent functional interface which is a way to define a function type but with a name,
should we do the same thing with tuples, by example provide a new keyword tuple (that works like a value record).

tuple MinMax(int min, int max);

MinMax minmax(int value1, int value2) {
  if (value1 < value2) {
    return (value1, value2);
  }
  return (value2, value1);
}  

in that case we can pattern match on tuples if the target type is available

MinMax result = ...
switch(result) {
  case (res1, res2) -> res1 + res2;
  case (0, 0) -> throw ...;
}

regards,
Rémi

----- Mail original -----
> De: "Cyrill Brunner" <cyrill.brunner at hispeed.ch>
> À: "Amber Expert Group Observers" <amber-spec-observers at openjdk.java.net>, "Remi Forax" <forax at univ-mlv.fr>, "jeremy a
> barrow" <jeremy.a.barrow at gmail.com>
> Envoyé: Mardi 21 Août 2018 16:03:14
> Objet: Re: Small things Java lets you do, but not everywhere.

>> - array literals
>> we already have them no ?
>> new int[] { 2, 3, 5};
>> if you mean supported in the bytecode, yes, we are working on that as a follow
>> up of ConstantDynamic.
>>
>>
> Seeing as they explicitly mentioned "I can imagine `return` statements
> working really well with it, ...", I think they refer to the shorthand
> instead.
> i.e. That you can do this:
> 
> int[] a = {2, 3};
> 
> But can't do any of these:
> 
> int[] a; a = {2, 3};
> 
> int[] f() { return {2, 3}; }
> 
> void g(int[] a) {} g({2, 3});
> 
> For all of these cases and possibly more, the shorthand literals are not
> allowed and explicit type information has to be added. To their point
> about expression switch, I think it's to allow this:
> 
> break {2, 3};
> 
> But with moving closer to patterns at a later stage, I think this would
> also be helpful
> 
> switch(...) {
>   case {2, 3} -> ...;
>   default -> ...;
> }
> 
> Regards,
> Cyrill Brunner


More information about the valhalla-spec-observers mailing list