You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Guy Matz <gu...@gmail.com> on 2016/05/03 21:58:08 UTC

Groovy defaults method argument to null?

I have a method that starts like this:

class Sidecar {

  def static String getVersions(String render_server) {

    def rest = new RESTClient('http://' + render_server)

    try {

      def vpath = *'*/r/api/versions'
      log.debug "Hitting http://${render_server}${vpath}"
      def resp = rest.get(path:vpath)


and when I call it as Sidecar.getVersions('server-name') it works as
expected.  When I "accidentally" call it without a parameter, though,
Groovy does not complain until it attempts the REST call:

groovy:000> r = Sidecar.getVersions()

15:49:41.209 [main] DEBUG com.jcrew.jenkins.helpers.Sidecar - Hitting
http://*null*/r/api/versions


Is Groovy defaulting the parameter to null when it's not supplied?

Re: Groovy defaults method argument to null?

Posted by Guy Matz <gu...@gmail.com>.
Annoying.  Thank you!!

So do you people generally leave the single-parameter methods with the
default Groovy behavior?  Or do y'all use the mandatory method trick in the
link above?  Or is there a better way?

Thanks again!

On Tue, May 3, 2016 at 5:23 PM, Shil Sinha <sh...@gmail.com> wrote:

> Yes, though this is specific to methods with one parameter and only occurs
> if the method call is not statically compiled. See
> http://glaforge.appspot.com/article/groovy-default-params-to-avoid-one-argument-methods-being-called-without-params for
> a discussion.
>
> On Tue, May 3, 2016 at 3:58 PM Guy Matz <gu...@gmail.com> wrote:
>
>> I have a method that starts like this:
>>
>> class Sidecar {
>>
>>   def static String getVersions(String render_server) {
>>
>>     def rest = new RESTClient('http://' + render_server)
>>
>>     try {
>>
>>       def vpath = *'*/r/api/versions'
>>       log.debug "Hitting http://${render_server}${vpath}"
>>       def resp = rest.get(path:vpath)
>>
>>
>> and when I call it as Sidecar.getVersions('server-name') it works as expected.  When I "accidentally" call it without a parameter, though, Groovy does not complain until it attempts the REST call:
>>
>> groovy:000> r = Sidecar.getVersions()
>>
>> 15:49:41.209 [main] DEBUG com.jcrew.jenkins.helpers.Sidecar - Hitting http://*null*/r/api/versions
>>
>>
>> Is Groovy defaulting the parameter to null when it's not supplied?
>>
>>

Re: Groovy defaults method argument to null?

Posted by Shil Sinha <sh...@gmail.com>.
Yes, though this is specific to methods with one parameter and only occurs
if the method call is not statically compiled. See
http://glaforge.appspot.com/article/groovy-default-params-to-avoid-one-argument-methods-being-called-without-params
for
a discussion.

On Tue, May 3, 2016 at 3:58 PM Guy Matz <gu...@gmail.com> wrote:

> I have a method that starts like this:
>
> class Sidecar {
>
>   def static String getVersions(String render_server) {
>
>     def rest = new RESTClient('http://' + render_server)
>
>     try {
>
>       def vpath = *'*/r/api/versions'
>       log.debug "Hitting http://${render_server}${vpath}"
>       def resp = rest.get(path:vpath)
>
>
> and when I call it as Sidecar.getVersions('server-name') it works as expected.  When I "accidentally" call it without a parameter, though, Groovy does not complain until it attempts the REST call:
>
> groovy:000> r = Sidecar.getVersions()
>
> 15:49:41.209 [main] DEBUG com.jcrew.jenkins.helpers.Sidecar - Hitting http://*null*/r/api/versions
>
>
> Is Groovy defaulting the parameter to null when it's not supplied?
>
>