Sorry, after reading that part of documentation I still feel a bit confused.

groovy:000> "a${x->println x}"
a
a
===> a
groovy:000> a = {x->println x}
===> groovysh_evaluate$_run_closure1@737a135b
groovy:000> a("a${x->println x}")
a
a
===> null

Say "a${a}" will be expanded as "a${x->println x}" and I don't understand why x 
captured the value "a" ?

"a${x->println x}" will output "a" because ${x->println x} will be lazy 
evaluated. Then the result "a" is magically captured by the closure {x->println 
x} and it print another "a". But if it like so then the first return ===> 
should be null.



> On 20 Jan 2017, at 16:49, Guillaume Laforge <[email protected]> wrote:
> 
> Hi Yang,
> 
> Interpolated values in a GString that are closures are executed.
> Have a look at the documentation which shows some examples of this:
> http://groovy-lang.org/syntax.html#_special_case_of_interpolating_closure_expressions
>  
> <http://groovy-lang.org/syntax.html#_special_case_of_interpolating_closure_expressions>
> 
> Guillaume
> 
> 
> On Fri, Jan 20, 2017 at 8:22 AM, Yang Yang <[email protected] 
> <mailto:[email protected]>> wrote:
> Script:
> a = {x -> println x}
> a("a${a}")
> 
> Output:
> a
> a
> 
> Why?
> 
> 
> 
> -- 
> Guillaume Laforge
> Apache Groovy committer & PMC Vice-President
> Developer Advocate @ Google Cloud Platform
> 
> Blog: http://glaforge.appspot.com/ <http://glaforge.appspot.com/>
> Social: @glaforge <http://twitter.com/glaforge> / Google+ 
> <https://plus.google.com/u/0/114130972232398734985/posts>

Reply via email to