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

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -