|
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. The comma operator would also conflict with tuples, which are also planned. See also the GEP as it currently stands: http://docs.codehaus.org/display/GROOVY/Multiple+Assignment+Proposal 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 This code doesn't work: Environment:
Error: 1 error </error> it's now: def a = 1 to be working. However, I think def (head, tail) = [1,2,3,4] head should be 1 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. My last comment and Jochen's last example briefly show what is currently possible or check out the following test: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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]