1. Is there a concise groovy syntax we could use that would optionally include or exclude a single parameter to the method?

Yes varags. Combine varag and map if named parameter is also required; varag with Object[] args parameter does the job

2."What we've discovered from testing is that if we send a value as either "true" or "false", it acts as if we sent "true"."

Yes You are right. The work around is keep the boolean as the last.

Please check,Sir, and correct me if I am wrong.

3.Still your implementation of function is not given, Sir.

T.Rathinavelu

 

Sent from Mail for Windows 10

 

From: David Karr
Sent: Sunday, April 26, 2020 11:34 AM
To: users@groovy.apache.org
Subject: Re: Re: Strategy for optionally excluding a named method parameter?

 

 

On Sat, Apr 25, 2020 at 10:42 PM Rathinavelu <rathinav...@gmail.com> wrote:

Sir,  I do not understand the NEED for your explanation,Sir for I have not asked for one!!. Please go through original trigger.

Mr.Karr was discussing   correctly

1. misbehavior of boolean  parameter in the middle of 24 parameters. He is correct.

 

If Boolean is lonely param it works OK. But Mr.Karr’s situation is real use case. In the middle both false and true evaluate as false as Mr.Karr correctly points out.

 

So an work around if the Boolean is last the situation is Ok

2. calling a function with any number of parameters  involves varag; calling with named parameters involves   map. They are combined here.

 

Those are the problems addressed. First line of my mail

Both named and varag

says that.

 

Frankly, I can't understand your point at all.  At first, I thought you didn't understand my problem, and were pointing out irrelevant facts. This reply makes me think that perhaps you noticed some indication of something actually going wrong, but I really can't tell.  Please try again to describe what I have "correctly pointed out".

 

Sent: Sunday, April 26, 2020 9:00 AM
To: users@groovy.apache.org; David Karr
Subject: Re: Strategy for optionally excluding a named method parameter?

 

It’s doing what you programmed it to do.

 

You’re doing a “println” on the first arg (args.one), then a print on the second arg (args.two.

 

So on your first 2 calls…

 

f(one: “string”, two: false), prints

     string (the line fieed in println)

     false (no linefeed)

 

then f(one: “string”, two: true), prints

                                       string (linefeed)

      true

 

So if you combine the outputs, it’s indeed

 

     string (the line fieed in println)

     false (print, no linefeed) string (linefeed)

      true

 

So. w/o the explanations,  looks exactly what you have

     string

     false string

     true

 

From: Rathinavelu <rathinav...@gmail.com>
Reply-To: <users@groovy.apache.org>
Date: Saturday, April 25, 2020 at 9:23 PM
To: "users@groovy.apache.org" <users@groovy.apache.org>, "users@groovy.apache.org" <users@groovy.apache.org>, David Karr <davidmichaelk...@gmail.com>
Subject: RE: Re: Strategy for optionally excluding a named method parameter?

 

Both named and varag

 

def f(Object[] args){ println args.one

print args.two }

f(one: "string",two:false)

f(one:"string", two:true)

f(two:false)

f(two:true)

f(two: 1==2)

f(two: 1==1)

f(two: 1==2)

 

[string]

[false][string]

[true][null]

[false][null]

[true][null]

[false][null]

[true][null]

[false]

 

Please as a work around keep the boolean as the last parameter. Since map is a subclass of  Object, the function polymorphically accepts map object . But why Boolean be the last to work? Sorry, Sir! I am into groovy only from last week! A long way to go!

The reason for always ‘false’ in the middle perhaps  is “invisible input”. Last output has no [null]

T.Rathinavelu

 

Sent from Mail for Windows 10

 

From: MG
Sent: Sunday, April 26, 2020 5:00 AM
To: users@groovy.apache.org; David Karr
Subject: Re: Strategy for optionally excluding a named method parameter?

 

Hi David,

since, as was mentioned, named parameters are implemented as a map underneath in Groovy, you should be able to remove the map entry of the parameter you do not want to pass, though I did not try this myself...

Cheers,
mg

On 25/04/2020 20:57, David Karr wrote:

 

 

On Sat, Apr 25, 2020 at 2:00 AM Rathinavelu <rathinav...@gmail.com> wrote:

Named parameters are not available in Groovy, say, as in Python., though they say it is. Groovy has only mapped parameters. The earlier mail works for a single ‘named’ parameter; if there are more parameters Groovy does not work as ‘expected’; it treats them only as positional parameters.

Kindly mail me an use-case.

T.Rathinavelu

 

The code sample looks something like this:

 

    functionName param1: value,

        param2: value,

        param3: value,

        param4: value,

        ...

        param25: value

 

For instance, we need to make either the previous call or the following:

 

    functionName param1: value,

        param2: value,

        param3: value,

        ...

        param25: value

 

Where the "param4" key and value are not provided.  Presently, we have an "if" checking for a condition, followed by the "true" block with the first version of the function call with 25 parameters, followed by the "else" and the "false" block with the second version, which has 24 parameters, all the same values as in the first block, except for the one key and value not provided in the second version.

 

 

Sent from Mail for Windows 10

 

From: David Karr
Sent: Saturday, April 25, 2020 1:48 AM
To: users@groovy.apache.org
Subject: Strategy for optionally excluding a named method parameter?

 

Lately my only Groovy work is scripted pipelines in Jenkins, version 2.89.4 .

 

I'm working with an api that is somewhat dumb in one respect.  The method we call takes ~25 parameters.  We send them as named parameters. One of the parameters is of boolean type.  What we've discovered from testing is that if we send a value as either "true" or "false", it acts as if we sent "true".  If we construct the call without that parameter entirely, it acts as if we sent "false". I tried making it send null, but that just causes it to fail at runtime.  We presently have an "if" for that one flag, with two calls to the method, one taking 25 parameters, the other taking 24.  It is really obnoxious.

 

Obviously, the proper fix is to change their api so that it works correctly.  The reality is, that's not going to happen any time soon in geological terms.

 

Is there a concise groovy syntax we could use that would optionally include or exclude a single parameter to the method?

 

 

 

 

 

 

Reply via email to