JDK-6573237 : Functions that return multiple values
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 7
  • Priority: P5
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_10
  • CPU: sparc
  • Submitted: 2007-06-25
  • Updated: 2011-02-16
  • Resolved: 2007-06-25
Related Reports
Duplicate :  
Relates :  
Relates :  
Relates :  
Description
A DESCRIPTION OF THE REQUEST :
I would like to reopen discussion about requests regarding functions that return multiple values . This is in my opinion one of the most missing features in Java language. There were great proposals for syntax and implementation collected in the following feature requests 4639379,
4034116, 4222792, which this one is duplicate of.

JUSTIFICATION :
There was a lots of discussion about potential compatibility issues, etc. a lots of proposals how to get around it. In my opinion this feature is at the same level as auto boxing/unboxing implemented lately years after the original proposal was submitted. Thats why I would like to reopen the discussion about this feature.

It can be implemented for example similar to auto boxing/unboxing by compiler using anonymous classes or possibly object collections or object arrays (which is what most developers end up doing anyway) without needing to change the format of the byte code.

The compiler can generate this boiler plate code for the developers and this syntax will greatly simplify coding of many tasks, decreases the amount of boiler plate code that needs to be written, decreases the effort to maintain the boiler-plate code, simplifies the code itself and increases its' readability.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I particularly like the syntax

public (int, File) getData(int p_stuff){
  return m_anInt, m_aFile;
}

...

File f; int i;
(i, f) = getData(2);

Comments
EVALUATION There is an open request for this already, which is not mentioned here: 4748349. The most analogous feature is not autoboxing but varargs. For migration compatibility, a method can start to use varargs in its formal parameters and existing callers will work fine. That is not apparent with multiple return values; one could imagine coercing a caller that expects an int from a method to accept an (int, File) pair, but developing sane rules will be tricky. (One might also imagine that not having access to the returned File makes further calls pointless in practice.) In the worst case, only methods written after JDK 'n' could return multiple values or call methods that return them. Varargs also affect overload resolution, as multiple return values would, but do not affect overriding, which multiple return values would affect because of covariant overriding of return types. The complexity level is higher than you think.
25-06-2007