Re: Default value in Closure

Paul King <[email protected]>
Newsgroups gmane.comp.lang.groovy.user
Message-ID <CAMbkE7SoyVUtVnvhzWKs9iy3eb9fo07MGWOimoObHfxHsmmpgw@mail.gmail.com>
Perhaps the documentation around methods/constructors[1] is clearest:
"Parameters are dropped from the right, however mandatory parameters
are never dropped."

So the following is fine (showing it's not just the last parameter):

def clos = { a, b = 2, c = 100 -> a * b + c }
assert clos(5, 4, 1) == 21
assert clos(5, 4) == 120
assert clos(5) == 110

The Closure documentation[2] indicates that it follows the same
principle as methods but perhaps
could be restated to make it clearer.

Cheers, Paul.

===========

[1] Method doco:
https://docs.groovy-lang.org/latest/html/documentation/#_default_arguments
[2] Closure doco:
https://docs.groovy-lang.org/latest/html/documentation/#_normal_parameters


On Wed, Mar 30, 2022 at 1:36 PM Niranjan Ghule <[email protected]> wrote:
>
> Hi Team,
>
> In Closure with default values in parameters, only the last parameter can have a default value, isn't it?
>
> We can add this detail in documentation.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.