jira.codehaus.org

  • Log In Access more options
    • Online Help
    • Keyboard Shortcuts
    • About JIRA
    • JIRA Credits
    • What?s New
  • Dashboards Access more options (Alt+d)
  • Projects Access more options (Alt+p)
  • Issues Access more options (Alt+i)
Signup
groovy
  • groovy
  • GROOVY-158

Multiple assignment

  • Log In
  • Views
    • XML
    • Word
    • Printable

Details

  • Type: New Feature New Feature
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: None
  • Fix Version/s: 1.6-rc-2
  • Component/s: GEP
  • Labels:
    None
  • Number of attachments :
    0

Description

Support for multiple assignment to simulate multiple return types:

a, b, c = someFunctionReturningAListOfThreeElements()

If more elements are returned than the number of variables to assign values to, we could get the head of list in the first variable and the rest of the list in the second variable:

head, tail = [1,2,3,4]
assert head == 1
assert tail == [2,3,4]

Can also be used for variable swapping
x, y = [y, x]
x, y = y, x

We have to be careful with varibale definitions, because currently:
def a, b, c = someFunctionReturningList()
because currently, a and b would be null, while c would be assigned to the value of the return of the function.

A GEP should be created to present all the possibilities, syntax, edge cases.

Further ideas (subsequent feature enhancements) could be considered like fetching matching groups from regex:
def regex = ~/firstname: (.), name: (.)/
firstname, name = ("firstname: Guillaume, name: Laforge") =~ regex)
assert firstname == "Guillaume"
assert name == "Laforge"

Issue Links

is depended upon by

Task - A task that needs to be done. GROOVY-762 implement syntax improvements

  • Major - Major loss of function.
  • Closed - The issue is considered finished, the resolution is correct. Issues which are not closed can be reopened.
relates to

New Feature - A new feature of the product, which has yet to be developed. GROOVY-1543 Support multiple declarations at once: def a,b = 1,2

  • Major - Major loss of function.
  • Closed - The issue is considered finished, the resolution is correct. Issues which are not closed can be reopened.

Activity

Ascending order - Click to sort in descending order
  • All
  • Comments
  • Work Log
  • History
  • Activity
james strachan made changes - 27/Jan/04 6:47 AM
Field Original Value New Value
Fix Version/s 1.0-final [ 10244 ]
Hide
Permalink
james strachan added a comment - 16/Feb/04 2:11 AM

We should also expand this to include list indexing for the lvalue. e.g.

list[a, b, c] = someListExpr

would expand to

t = someListExpr
list[a] = t[0]
list[b] = t[1]
list[c] = t[2]

Show
james strachan added a comment - 16/Feb/04 2:11 AM We should also expand this to include list indexing for the lvalue. e.g. list [a, b, c] = someListExpr would expand to t = someListExpr list [a] = t [0] list [b] = t [1] list [c] = t [2]
Hide
Permalink
John Stump added a comment - 21/Jul/04 11:12 AM

Your initial syntax implies that groovy will never support the comma operator, because if we did there might be a conflict. I personally never use the comma operator in Java except in for() loops, but as I understand it, Groovy will someday support the standard for() loop.

Show
John Stump added a comment - 21/Jul/04 11:12 AM Your initial syntax implies that groovy will never support the comma operator, because if we did there might be a conflict. I personally never use the comma operator in Java except in for() loops, but as I understand it, Groovy will someday support the standard for() loop.
blackdrag blackdrag made changes - 04/Mar/05 10:23 AM
Link This issue is depended upon by GROOVY-753 [ GROOVY-753 ]
blackdrag blackdrag made changes - 04/Mar/05 10:51 AM
Link This issue is depended upon by GROOVY-753 [ GROOVY-753 ]
blackdrag blackdrag made changes - 04/Mar/05 10:51 AM
Link This issue is depended upon by GROOVY-762 [ GROOVY-762 ]
Hide
Permalink
chocolateboy added a comment - 26/Jun/05 8:36 PM

The comma operator would also conflict with tuples, which are also planned.

Show
chocolateboy added a comment - 26/Jun/05 8:36 PM The comma operator would also conflict with tuples, which are also planned.
Guillaume Laforge made changes - 20/Jan/07 3:21 PM
Fix Version/s 1.1 [ 10436 ]
Fix Version/s 1.0 [ 10244 ]
Guillaume Laforge made changes - 13/Feb/07 7:48 AM
Fix Version/s 1.1-beta-2 [ 10436 ]
Guillaume Laforge made changes - 13/Feb/07 10:14 AM
Issue Type Wish [ 5 ] New Feature [ 2 ]
Summary allow x, y, z = [listExpression] to be used for multiple assignment Multiple assignment
Fix Version/s 1.1 [ 13166 ]
Description to simulate multiple return types. Can also be used for variable swapping

x, y = [y, x]
Support for multiple assignment to simulate multiple return types:

a, b, c = someFunctionReturningAListOfThreeElements()

If more elements are returned than the number of variables to assign values to, we could get the head of list in the first variable and the rest of the list in the second variable:

head, tail = [1,2,3,4]
assert head == 1
assert tail == [2,3,4]

Can also be used for variable swapping
x, y = [y, x]
x, y = y, x

We have to be careful with varibale definitions, because currently:
def a, b, c = someFunctionReturningList()
because currently, a and b would be null, while c would be assigned to the value of the return of the function.

A GEP should be created to present all the possibilities, syntax, edge cases.


Further ideas (subsequent feature enhancements) could be considered like fetching matching groups from regex:
def regex = ~/firstname: (.*), name: (.*)/
firstname, name = ("firstname: Guillaume, name: Laforge") =~ regex)
assert firstname == "Guillaume"
assert name == "Laforge"
Guillaume Laforge made changes - 06/Oct/07 4:56 PM
Fix Version/s 2.0 [ 13489 ]
Fix Version/s 1.1 [ 13166 ]
Hide
Permalink
Paul King added a comment - 30/Nov/07 9:04 PM - edited

See also the GEP as it currently stands: http://docs.codehaus.org/display/GROOVY/Multiple+Assignment+Proposal

Show
Paul King added a comment - 30/Nov/07 9:04 PM - edited See also the GEP as it currently stands: http://docs.codehaus.org/display/GROOVY/Multiple+Assignment+Proposal
Paul King made changes - 06/Jan/08 1:46 AM
Link This issue relates to GROOVY-1543 [ GROOVY-1543 ]
Paul King made changes - 10/Jan/08 2:54 AM
Component/s GEP [ 12771 ]
Hide
Permalink
blackdrag blackdrag added a comment - 22/Apr/08 7:13 AM

since there was low feedback on this the only form that is supported atm is

a,b = [1,2]

meaning no declaration and the number of arguments must match

Show
blackdrag blackdrag added a comment - 22/Apr/08 7:13 AM since there was low feedback on this the only form that is supported atm is a,b = [1,2] meaning no declaration and the number of arguments must match
blackdrag blackdrag made changes - 22/Apr/08 7:13 AM
Fix Version/s 1.6 [ 13832 ]
Fix Version/s 2.0 [ 13489 ]
Resolution Fixed [ 1 ]
Status Open [ 1 ] Closed [ 6 ]
Assignee Jochen Theodorou [ blackdrag ]
Hide
Permalink
Paul King added a comment - 22/Apr/08 6:32 PM

Just as a comment for future Jira searchers. This does allow Groovy to do the often discussed swap two variables without a temp variable! E.g.:

def a = 1
def b = 2
[a, b] = [b, a]
println a // => 2
println b // => 1
Show
Paul King added a comment - 22/Apr/08 6:32 PM Just as a comment for future Jira searchers. This does allow Groovy to do the often discussed swap two variables without a temp variable! E.g.: def a = 1 def b = 2 [a, b] = [b, a] println a // => 2 println b // => 1
Hide
Permalink
Cazacu Mihai added a comment - 21/Feb/09 5:14 AM

This code doesn't work:
<code>
def a = 1
def b = 2
[a, b] = [b, a]
println a // => 2
println b // => 1
</code>

Environment:

  • Groovy Version: 1.6.0 JVM: 1.6.0_12

Error:
<error>
class org.codehaus.groovy.ast.expr.ListExpression, with its value '[a, b]', is a bad expression as the left hand side of an assignment operator at line: 4 column: 8. File: /home/ms/work/groovy/test04.groovy @ line 4, column 8.
[a, b] = [b, a]
^

1 error

</error>

Show
Cazacu Mihai added a comment - 21/Feb/09 5:14 AM This code doesn't work: <code> def a = 1 def b = 2 [a, b] = [b, a] println a // => 2 println b // => 1 </code> Environment: Groovy Version: 1.6.0 JVM: 1.6.0_12 Error: <error> class org.codehaus.groovy.ast.expr.ListExpression, with its value ' [a, b] ', is a bad expression as the left hand side of an assignment operator at line: 4 column: 8. File: /home/ms/work/groovy/test04.groovy @ line 4, column 8. [a, b] = [b, a] ^ 1 error </error>
Hide
Permalink
Chanwit Kaewkasi added a comment - 21/Feb/09 5:56 AM

per http://jira.codehaus.org/browse/GROOVY-1543?focusedCommentId=150396&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_150396

it's now:

def a = 1
def b = 2
(a,b) = [b, a]

to be working.

However, I think

def (head, tail) = [1,2,3,4]
would work as in the issue descrption:

head should be 1
and tail should be [2,3,4]

Show
Chanwit Kaewkasi added a comment - 21/Feb/09 5:56 AM per http://jira.codehaus.org/browse/GROOVY-1543?focusedCommentId=150396&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_150396 it's now: def a = 1 def b = 2 (a,b) = [b, a] to be working. However, I think def (head, tail) = [1,2,3,4] would work as in the issue descrption: head should be 1 and tail should be [2,3,4]
Hide
Permalink
Paul King added a comment - 21/Feb/09 6:32 AM

Current behavior doesn't have special head/tail treatment.

(one, two) = "The quick brown fox".tokenize()
assert one == 'The'
assert two == 'quick'
Show
Paul King added a comment - 21/Feb/09 6:32 AM Current behavior doesn't have special head/tail treatment. (one, two) = "The quick brown fox" .tokenize() assert one == 'The' assert two == 'quick'
Hide
Permalink
blackdrag blackdrag added a comment - 21/Feb/09 7:21 AM

in discussion was to mark an element to take the surplus parts as list.. for example like:

(head, *tail) = [1,2,3,4]
assert head == 1
assert tail = [2,3,4]

(*head, tail) = [1,2,3,4]
assert head == [1,2,3]
assert tail = 4

(head, *middle, end) = [1,2,3,4]
assert head == 1
assert middle = [2,3]
assert end == 4

I thought about doing maybe even something like this:

(*a,3,*b) = [1,2,3,4]
assert a == [1,2]
assert b == [4]

But that is just an idea, not really discussed and I need use cases for these first. And normally you don't look for use cases because you want to have a feature, instead you have a problem and think of a feature to solve that use case.

Basically you can have these cases

(a,b) = [1,2]     // b== 2, or b== [2] ?
(a,b) = [1]        // exception, or b==[], or b==null?
(a,b) = [1,2,3]  // b==2, or b==[2,3], or exception?

I decided for b==2, exception, b==2. But any other version seems to be equally reasonable. With a fix head/tail logic for example I would expect b==[2], b==[], b==[2,3]. But is this really multiple assignment? It looks more like a combined car/cdr operation, which does not exactly fit into Groovy atm.

If you are interested in getting head tail logic into groovy, then I suggest you start a thread on the user list for it.

Show
blackdrag blackdrag added a comment - 21/Feb/09 7:21 AM in discussion was to mark an element to take the surplus parts as list.. for example like: (head, *tail) = [1,2,3,4] assert head == 1 assert tail = [2,3,4] (*head, tail) = [1,2,3,4] assert head == [1,2,3] assert tail = 4 (head, *middle, end) = [1,2,3,4] assert head == 1 assert middle = [2,3] assert end == 4 I thought about doing maybe even something like this: (*a,3,*b) = [1,2,3,4] assert a == [1,2] assert b == [4] But that is just an idea, not really discussed and I need use cases for these first. And normally you don't look for use cases because you want to have a feature, instead you have a problem and think of a feature to solve that use case. Basically you can have these cases (a,b) = [1,2] // b== 2, or b== [2] ? (a,b) = [1] // exception, or b==[], or b== null ? (a,b) = [1,2,3] // b==2, or b==[2,3], or exception? I decided for b==2, exception, b==2. But any other version seems to be equally reasonable. With a fix head/tail logic for example I would expect b== [2] , b==[], b== [2,3] . But is this really multiple assignment? It looks more like a combined car/cdr operation, which does not exactly fit into Groovy atm. If you are interested in getting head tail logic into groovy, then I suggest you start a thread on the user list for it.
Hide
Permalink
codecraig added a comment - 24/Feb/09 6:41 PM

So what's the final result, what's been provided for "multiple assignment"?

Show
codecraig added a comment - 24/Feb/09 6:41 PM So what's the final result, what's been provided for "multiple assignment"?
Hide
Permalink
Paul King added a comment - 24/Feb/09 8:31 PM

My last comment and Jochen's last example briefly show what is currently possible or check out the following test:
http://svn.groovy.codehaus.org/browse/~raw,r=13691/groovy/trunk/groovy/groovy-core/src/test/gls/statements/MultipleAssignmentDeclarationTest.groovy

Show
Paul King added a comment - 24/Feb/09 8:31 PM My last comment and Jochen's last example briefly show what is currently possible or check out the following test: http://svn.groovy.codehaus.org/browse/~raw,r=13691/groovy/trunk/groovy/groovy-core/src/test/gls/statements/MultipleAssignmentDeclarationTest.groovy
Hide
Permalink
Paul King added a comment - 24/Feb/09 8:35 PM

Or this test:
http://svn.groovy.codehaus.org/browse/~raw,r=13691/groovy/trunk/groovy/groovy-core/src/test/gls/statements/MultipleAssignmentTest.groovy

Show
Paul King added a comment - 24/Feb/09 8:35 PM Or this test: http://svn.groovy.codehaus.org/browse/~raw,r=13691/groovy/trunk/groovy/groovy-core/src/test/gls/statements/MultipleAssignmentTest.groovy
Hide
Permalink
codecraig added a comment - 24/Feb/09 8:58 PM

Perfect, thanks.

Show
codecraig added a comment - 24/Feb/09 8:58 PM Perfect, thanks.

People

  • Assignee:
    blackdrag blackdrag
    Reporter:
    james strachan
Vote (2)
Watch (3)

Dates

  • Created:
    27/Jan/04 6:46 AM
    Updated:
    24/Feb/09 8:58 PM
    Resolved:
    22/Apr/08 7:13 AM
  • Atlassian JIRA (v5.2.7#850-sha1:b2af0c8)
  • Report a problem
  • Powered by a free Atlassian JIRA open source license for Codehaus. Try JIRA - bug tracking software for your team.