[英]Groovy : add root closure if condition
A little bit confuse about this simple use case :对这个简单的用例有点困惑:
I want to add a closure around an other one only with conditions.我只想在有条件的情况下在另一个周围添加一个闭包。
For the moment, I only succeed to do this :目前,我只能成功做到这一点:
if(condition) {
my_root_closure {
my_main_closure {
do_stuff()
}
}
} else {
my_main_closure {
do_stuff()
}
}
I would like to do this without repeat the my_main_closure bloc.我想在不重复 my_main_closure 块的情况下执行此操作。
To avoid repetition, you could create new closure that calls my_main_closure
and store it in a variable:为避免重复,您可以创建新的闭包来调用my_main_closure
并将其存储在一个变量中:
def mmc = {
my_main_closure {
do_stuff()
}
}
if(condition) {
my_root_closure( mmc )
} else {
mmc()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.