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
-
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]