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

Capture and play voice with Asterisk ARI -

java - Why database contraints in HSQLDB are only checked during a commit when using transactions in Hibernate? -

visual studio - Installing Packages through Nuget - "Central Directory corrupt" -