title: named parameters with default values in groovy
body: <p>Is it possible to have named parameters with default values in groovy? My plan is to make a sort of object factory, which can be called with no arguments at all in order to get an object with default values. Also, I'd need the functionality to explicitly set any of the params for the object. I believe this is possible with Python keyword arguments, for example.</p>
  
  <p>The code I'm attempting with right now is something like below</p>
  
  <pre><code>// Factory method
  def createFoo( name='John Doe', age=51, address='High Street 11') {
    return new Foo( name, age, address )
  }
  
  // Calls
  Foo foo1 = createFoo()  // Create Foo with default values
  Foo foo2 = createFoo( age:21 )  // Create Foo where age param differs from defaut
  Foo foo3 = createFoo( name:'Jane', address:'Low Street 11' )  // You get the picture
  // + any other combination available
  </code></pre>
  
  <p>The real app that I'm working on will have a lot more of parameters and thus a lot more combinations needed.</p>
  
  <p>Thanks</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>The factory method I'm planning is for testing purposes. Cannot really touch the actual Foo class and especially not it's default values.</p>
  
  <p>@dmahapatro and @codelarks answere below had a good point in using a Map as a param that gave me an idea of a possible solution. I could create a map with the wanted defaults and override the needed values, and pass that to the factory method. This'll probably do the job and I'll go with that, unless I get a hint of a better approach.</p>
  
  <p>My current approach below</p>
  
  <pre><code>defaults = [ name:'john', age:61, address:'High Street']
  
  @ToString(includeFields = true, includeNames = true)
  class Foo {
    // Can't touch this :)
    def name = ''
    def age = 0
    def address = ''
  }
  
  def createFoo( Map params ) {
    return new Foo( params )
  }
  
  println createFoo( defaults )
  println createFoo( defaults &lt;&lt; [age:21] )
  println createFoo( defaults &lt;&lt; [ name:'Jane', address:'Low Street'] )
  </code></pre>
  
  <p><strong>NOTE:</strong> leftShift operation ( &lt;&lt; ) modifies the the original map, so in the above example age will be 21 in the last method call as well. In my case, this is not a problem as the defaults map can be created freshly each time in setup method.</p>