Groovy Closure with optional arguments

孤街浪徒 提交于 2019-12-09 07:48:00

问题


I want to define a closure which takes one argument (which i refer to with it ) sometimes i want to pass another additional argument to the closure. how can i do this?


回答1:


You could set the second argument to a default value (such as null):

def cl = { a, b=null ->
  if( b != null ) {
    print "Passed $b then "
  }
  println "Called with $a"
}

cl( 'Tim' )          // prints 'Called with Tim'
cl( 'Tim', 'Yates' ) // prints 'Passed Yates then Called with Tim

Another option would be to make b a vararg List like so:

def cl = { a, ...b ->
  if( b ) {
    print "Passed $b then "
  }
  println "Called with $a"
}

cl( 'Tim' )                    // prints 'Called with Tim'
cl( 'Tim', 'Yates' )           // prints 'Passed [Yates] then Called with Tim
cl( 'Tim', 'Yates', 'Groovy' ) // prints 'Passed [Yates, Groovy] then Called with Tim



回答2:


hopefully this helps

​def clr = {...a ->  
    print "Passed $a then "
    enter code here

}

​clr('Sagar')
clr('Sagar','Rahul')



回答3:


The variants from @tim_yates do not work with @TypeChecked (in a class context), at least with Groovy 2.4.11 where the default arg is ignored and does not compile :-(

So other (admittedly uglier) solutions that would work in this case are:

  1. declaring the closure first seems to work fine (necessary for recursions anyway):

    def cl
    cl = { ... }
    
    • at least in Eclipse Neon / Groovy-Eclipse Plugin 2.9.2 the code completion/suggestions do not work either way using the closure later on in the same code block => so nothing lost as far as I can say
  2. with @TypeChecked(value=TypeCheckingMode.SKIP) it would work for both, but then you would loose the type checking on the method (or class, depending on where you put it)

  3. declare closure delegate cl2:

    @TypeChecked
    class Foo { 
    
      static main( String[] args ) {
    
        def cl = { a, b ->
          if( b != null )
            print "Passed $b then "
          println "Called with $a"
        }
        def cl2 = { a -> cl( a, null ) }
    
        cl2( 'Tim' )         // prints 'Called with Tim'
        cl( 'Tim', 'Yates' ) // prints 'Passed Yates then Called with Tim           
      }
    }
    
  4. transform closure into class method, e.g.

    @TypeChecked
    class Foo { 
    
      cl( a, b=null ) {
        if( b != null )
          print "Passed $b then "
        println "Called with $a"
      }
    
      static main( String[] args ) {
        cl( 'Tim' )          // prints 'Called with Tim'
        cl( 'Tim', 'Yates' ) // prints 'Passed Yates then Called with Tim           
      }
    }
    


来源:https://stackoverflow.com/questions/12580262/groovy-closure-with-optional-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!