Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do Groovy closures declare a maximum, rather than constant number of parameters

When declaring a closure we can query it for the number of accepted parameters using:

Closure#getMaximumNumberOfParameters()

So for example:

def closure = { String param ->
}
println(closure.maximumNumberOfParameters)

Will output:

1

Why does the method declare the number of parameters as a maximum rather than a constant?

In what situation will the return value of this method differ from the actual number of parameters declared in the closure?

like image 800
noamt Avatar asked Sep 27 '22 16:09

noamt


1 Answers

Default parameters?

def closure = { String param = 'something' ->
}

So you can technically call

closure()

And

closure('something else')
like image 162
tim_yates Avatar answered Sep 29 '22 16:09

tim_yates