Why Groovy closure declares a maximum rather than a constant number of parameters

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

Closure#getMaximumNumberOfParameters() 

So for example:

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

It will display:

 1 

Why does the method declare the number of parameters as a maximum, and not a constant?

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

+5
source share
1 answer

Default options?

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

So you can technically call

 closure() 

and

 closure('something else') 
+4
source

All Articles