论坛首页 Java企业应用论坛

Groovy's HERE DOCUMENT and Closure

浏览 2449 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-05-09  
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 :)
   发表时间:2007-05-09  
这个语法实在是非同一般的恶心啊
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics