Details
-
Sub-task
-
Status: Open
-
Major
-
Resolution: Unresolved
-
1.7-beta-2
-
None
-
None
-
Patch
Description
When adding methods to classes through EMC, with the SomeClass.metaClass.someMethod notation, the delegate of the closure we assign to someMethod represents the instance of SomeClass. However, when adding methods on Closures through EMC, the delegate variable in the closure is not pointing at the closure instance itself, but the script or the surrounding class.
Example #1 in a class:
class Foo { def bar() { Closure.metaClass.baz = { -> assert delegate instanceof Closure } def c = {} c.baz() } } new Foo().bar()
In that case, the delegate is the new Foo() instance, instead of being the closure we assign to baz.
Example #2 in a script:
Closure.metaClass.baz = { -> assert delegate instanceof Closure } def c = {} c.baz()​
In that case, the delegate is the current script.