string - Groovy - A Tale of Two Closure Invocations -
this works:
def myclosure = { println 'hello world!' } 'myclosure'()
this not work:
def myclosure = { println 'hello world!' } string test = 'myclosure' test()
why, , there way make it?
with
test()
the parser evaluate call test
closure/method without evaluating variable first (otherwise couldn't call methods have variable of same name)
instead, try:
myclosure = { println 'hello world!' } string test = 'myclosure' "$test"()
edit -- class example
class test { def myclosure = { println "hello world" } void run( string closurename ) { "$closurename"() } static main( args ) { new test().run( 'myclosure' ) } }
edit -- class run closure example
class test { def myclosure = { println "hello world" } def run = { string closurename -> "$closurename"() } static main( args ) { new test().run( 'myclosure' ) } }
Comments
Post a Comment