首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用'it‘的Groovy嵌套闭包

使用'it‘的Groovy嵌套闭包
EN

Stack Overflow用户
提问于 2010-03-02 16:09:45
回答 2查看 30.6K关注 0票数 21

闭包中的代码可以引用it变量。

8.times { println it }

def mywith(Closure closure) {
   closure()
}

mywith { println it }

考虑到这种行为,您不能期望下面的代码打印0011

2.times {
   println it 

   mywith {
      println it
   }
}

相反,我不得不写

2.times { i ->
   println i 

   mywith {
      println i
   }
}

我的问题是:没有参数的闭包为什么要覆盖it变量,即使它们不需要它。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2361650

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档