Groovy: add a method to a closure -


i have following closure

def closure = {    println ("closure code") } 

and add method it. if try

 closure.metaclass.fun = { c->         c.call();         println ("extra code");  }    

i exception

groovy.lang.missingpropertyexception: no such property: fun class: org.codehaus.groovy.runtime.metaclass.closuremetaclass 

reading answer, blindly tried call

expandometaclass.enableglobally() 

but it's not working.

is there way achive want?

you can do:

def closure = {     println "closure code" }  closure.getmetaclass().fun = { ->     delegate.call()     println "extra code" }     closure.fun() 

which prints:

closure code code 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -