Re: Default value in Closure

Paul King <[email protected]>
Newsgroups gmane.comp.lang.groovy.user
Message-ID <CAMbkE7TG1q_hd7YhB-H3_U53gtvuSVdx+6e+FdsCQeA0Vk6CAg@mail.gmail.com>
Sounds like a good idea.

On Thu, Mar 31, 2022 at 3:21 AM Niranjan Ghule <[email protected]> wrote:
>
> Hi Paul,
>
> Thanks for sharing this.
>
> I agree with you on making it clearer by restating it perhaps by adding link you mentioned after the code snippet in documentation.
>
> I can raise a PR if you think it's a good idea.
>
>
> Thanks and Regards
> Niranjan
>
> On Wed, 30 Mar 2022, 5:32 pm Paul King, <[email protected]> wrote:
>>
>> 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.