`
bma
  • 浏览: 56602 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

Groovy's HERE DOCUMENT and Closure

阅读更多
Something fun with Groovy's 'HERE DOCUMENT', i.e., """..."""

In groovyConsole, execute (Ctrl + R) following code:
"""
${ println "Hi" }
"""

result: Hi
Cool, isn't it?

How about this?
"""
${
class Foo{}
}
"""
No way...Why?

The curly braces denote a closure,  So, we can put anything valid in a closure into the ${} within a HERE DOC:

a = [1,2,3,4,5,6]

"""
${
a.each {print it}
}
"""

result: 123456
Wow, closure within closure. So below is nothing strange:

"""
${
println """ ${println "Can you see me?"} """
}
"""

result: yes, I can :)
分享到:
评论
1 楼 kdekid 2007-05-09  
这个语法实在是非同一般的恶心啊

相关推荐

Global site tag (gtag.js) - Google Analytics