I want to return multiple values from a function written in groovy and receive them , but i am getting an 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
My code is
int a=10
int b=0
println "a is ${a} , b is ${b}"
[a,b]=f1(a)
println "a is NOW ${a} , b is NOW ${b}"
def f1(int x) {
return [a*10,a*20]
}
You almost have it. Conceptually
[ a, b ]creates a list, and( a, b )unwraps one, so you want(a,b)=f1(a)instead of[a,b]=f1(a).Another example returning objects, which don’t need to be the same type:
Additionally you can combine the declaration and assignment: