Quantcast

Calling constructor( Object ) with no params

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Calling constructor( Object ) with no params

tim yates-2
Spotted a question on Stackoverflow that surprised me today:


Basically this code:


class Test {
  String name

  Test( String s ) {
    this.name = s ?: 'tim'
  }

  void a( String prefix ) {
    prefix = prefix ?: 'Hello'
    println "$prefix $name"
  }
}

new Test().a()
Where the constructor call and the method call have no parameters both work (passing null)

Is this documented anywhere?  Or has it crept in from somewhere and will likely vanish in future versions?

Or have I just never seen it before, and it's been there from the start?

Cheers for any clarification,

Tim
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Calling constructor( Object ) with no params

Cédric Champeau
This is valid for any method call (not only constructors) and I (as well as others) really dislike this "feature" (because it's very error prone) so it will probably disappear in Groovy 3. Also, it's not supported by static compilation :)

Le 28/11/2012 12:41, Tim Yates a écrit :
Spotted a question on Stackoverflow that surprised me today:


Basically this code:

class Test {
  String name

  Test( String s ) 
{
    this.name = s ?: 'tim
'
  }

  void a( String prefix ) {
    prefix = prefix ?: 'Hello'
    println "$prefix $name"
  }
}

new Test().a()
Where the constructor call and the method call have no parameters both work (passing null)

Is this documented anywhere?  Or has it crept in from somewhere and will likely vanish in future versions?

Or have I just never seen it before, and it's been there from the start?

Cheers for any clarification,

Tim


-- 
Cédric Champeau
SpringSource - A Division Of VMware
http://www.springsource.com/
http://twitter.com/CedricChampeau
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Calling constructor( Object ) with no params

tim yates-2
Thanks Cédric :-)

On 28 November 2012 11:46, Cédric Champeau <[hidden email]> wrote:
This is valid for any method call (not only constructors) and I (as well as others) really dislike this "feature" (because it's very error prone) so it will probably disappear in Groovy 3. Also, it's not supported by static compilation :)

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Calling constructor( Object ) with no params

Kunal Dabir
I guess, probably this might have been there to support default values so that

def method(var) {
    println var
}

is same as 

def method(var=null) {
    println var
}


method(1) => 1
method() => null

If the support for implied null value of parameter would be removed from groovy 3 and if one doesn't declare the default value explicitly in method definition and call method() without param then will groovy  3 throw error

just a question, nothing against the idea.



On Wed, Nov 28, 2012 at 5:19 PM, Tim Yates <[hidden email]> wrote:
Thanks Cédric :-)


On 28 November 2012 11:46, Cédric Champeau <[hidden email]> wrote:
This is valid for any method call (not only constructors) and I (as well as others) really dislike this "feature" (because it's very error prone) so it will probably disappear in Groovy 3. Also, it's not supported by static compilation :)


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Calling constructor( Object ) with no params

Jochen Theodorou
Am 29.11.2012 05:09, schrieb Kunal Dabir:
[...]
> If the support for implied null value of parameter would be removed from
> groovy 3 and if one doesn't declare the default value explicitly in
> method definition and call *method()* without param then will groovy  3
> throw error?

yes

bye blackdrag

--
Jochen "blackdrag" Theodorou - Groovy Project Tech Lead
blog: http://blackdragsview.blogspot.com/
german groovy discussion newsgroup: de.comp.lang.misc
For Groovy programming sources visit http://groovy-lang.org


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Loading...