JDK-6664637 : Parallel assignment
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 7
  • Priority: P5
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2008-02-19
  • Updated: 2011-02-16
  • Resolved: 2008-02-20
Related Reports
Duplicate :  
Relates :  
Description
A DESCRIPTION OF THE REQUEST :
I would like to see Parallel assignment in the Java language. What is Parallel assignment?

It allows you to assign multiple variables at the same time with only one single statement.

Let me demonstrate:

int a = 5;
int b = 6;

a, b = b, a;

By executing this code the two variables should swap content in only one line of code. In this case it would be important that the swap happened parallel so that assigning a = b would not affect the b = a that would happen "at the same time".

Furthermore, by implementing this you could also open for multiple return values for methods:

int someMethod()
{
return 5, 6;
}

a, b = someMethod();

I would assume implementing either of these would imply that also the other one was implemented. As a result this bug is directly related to the following:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4748349

More about parallel assignment can be found here:
http://phrogz.net/ProgrammingRuby/tut_expressions.html#parallelassignment

--

I believe that there isnt even needed that much change in the JVM. Parallel assignment could for instance be "translated" to an array, or a struct/class so that the JVM only sees whats already in the specs. Like with enum, a new class could be generated by the compiler that holds the values.

JUSTIFICATION :
As more an more higher-level languages than java gets new features that are designed to reduce the amount of code and increase readability for the developers, Java keeps lagging behind by being conservative and refuse to make modifications in the language. The most used argument against new features are code bloat and that its "too late" for the class library. But there is no need to change the class library. This way both conservative java developers and the rest of us could enjoy the power of Java.

C# has recently gained a lot of new features including LINQ and so on, and have gained a huge fanbase as a result. This should be a living proof that it doesnt hurt to update the language in order to have an edge. I believe that while Java keeps lagging behind it could be highly beneficial for the language to gain appealing features like this that allow the developer to create better code. As far as I know, C# does not have this feature yet, but with all the interesting features I would believe that it would be another nail in the coffin for Java if they suddenly decided to implement it. Lets allow Java to have the advantage for once! Just because its possible doesnt mean that everyone is required to use it.

Ruby has it.
Python has it.
Perl has it.

Who is next?

Comments
EVALUATION This is really a request for tuples, as is 4748349. Note that generating an anonymous temporary array or class for the RHS values can cause an OutOfMemoryError; there's also an interaction with volatile variables which might forbid some multi-assignments. But these are implementation details; the real reason it's worth being sceptical over multi-assignment is that it's convenient to write but arguably hard to read. Trivial examples look appealing, especially when the temporary variable required for a swap is made implicit, but what about when there are five non-trivial and side-effecting expressions on the RHS? Even if the IDE matches the number of RHS expressions with the number of LHS variables, you still have to check by eye that a RHS expression is well-matched with its LHS variable. This is all a source of increased confusion, not increased expressiveness. Now, you could draw a parallel with matching expressions to variables in a method call, and even claim that this request is analogous to named actual parameters. But the implicit assignments in method call are morally quite different from the explicit assignments involved in this request, which if anything should be made harder rather than easier.
20-02-2008