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
Post a Comment