Reputation: 5409
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?
Upvotes: 24
Views: 14257
Reputation: 4755
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 = { ... }
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
}
}
Upvotes: 0
Reputation: 313
hopefully this helps
def clr = {...a ->
print "Passed $a then "
enter code here
}
clr('Sagar')
clr('Sagar','Rahul')
Upvotes: 3
Reputation: 171084
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
Upvotes: 42