Details
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
-
GROOVY-762
implement syntax improvements
-
- relates to
-
GROOVY-1543
Support multiple declarations at once: def a,b = 1,2
-
Activity
| Field | Original Value | New Value |
|---|---|---|
| Fix Version/s | 1.0-final [ 10244 ] |
| Link |
This issue is depended upon by |
| Link |
This issue is depended upon by |
| Link |
This issue is depended upon by |
| Fix Version/s | 1.1 [ 10436 ] | |
| Fix Version/s | 1.0 [ 10244 ] |
| Fix Version/s | 1.1-beta-2 [ 10436 ] |
| 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" |
| Fix Version/s | 2.0 [ 13489 ] | |
| Fix Version/s | 1.1 [ 13166 ] |
| Link |
This issue relates to |
| Component/s | GEP [ 12771 ] |
| Fix Version/s | 1.6 [ 13832 ] | |
| Fix Version/s | 2.0 [ 13489 ] | |
| Resolution | Fixed [ 1 ] | |
| Status | Open [ 1 ] | Closed [ 6 ] |
| Assignee | Jochen Theodorou [ blackdrag ] |
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]