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:
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
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)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 } }
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 } }