In Groovy, you can bound the arguments of a method call
to a list.
For example, in the below snippet, I defined a function
'sum' that takes two integers and return the sum of two integers. While calling
I used the spread operator to bound the list to the method arguments.
int sum(int a, int b){
return a +
b
}
def args1 = [2, 3]
int result1 = sum(*args1)
Find the below working application.
HelloWorld.groovy
int sum(int a, int b){ println "a : ${a}, b : ${b}" return a + b } int sum(int a, int b, int c){ println "a : ${a}, b : ${b}, c : ${c}" return a + b + c } def args1 = [2, 3] def args2 = [2, 3, 5] int result1 = sum(*args1) int result2 = sum(*args2) println "result1 : ${result1}" println "result2 : ${result2}"
Output
a : 2, b : 3
a : 2, b : 3, c : 5
result1 : 5
result2 : 10
No comments:
Post a Comment