You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (Jira)" <ji...@apache.org> on 2022/02/03 22:52:00 UTC

[jira] [Closed] (GROOVY-2641) Dynamically created closure handles delegate differently from statically created closure

     [ https://issues.apache.org/jira/browse/GROOVY-2641?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Paul King closed GROOVY-2641.
-----------------------------

> Dynamically created closure handles delegate differently from statically created closure
> ----------------------------------------------------------------------------------------
>
>                 Key: GROOVY-2641
>                 URL: https://issues.apache.org/jira/browse/GROOVY-2641
>             Project: Groovy
>          Issue Type: Sub-task
>          Components: groovy-runtime
>    Affects Versions: 1.5.4
>         Environment: CentOS 5 n Intel with java 1.6.0_03-b05
>            Reporter: Bruce Cota
>            Priority: Major
>
> The following script executes two closures.  One is statically defined in class Test, 
> the other is returned by Test.makeClosure().  In each case the delegate of the
> Closure is set to be an instance of class Delegate, but in one case a call to
> method "foo()" appears to resolve to Test.foo(), while in the second it appears
> to resolve to Delegate.foo().
> I showed this to Dierk at the G2 conference in Reston, and he asked me to post
> it as a bug.
> =====================================================================
> class Test {
>     def foo() {
>         "NOT the delegate"
>     }
>     def makeClosure() {
>         def closure = {
>             println foo()
>             println delegate.foo()
>         }
>     }
>     static staticClosure = {
>             println foo()
>             println delegate.foo()
>     }
> }
> class Delegate {
>     def foo() {
>         "delegate"
>     }
> }
> Closure dynamicClosure = new Test().makeClosure()
> dynamicClosure.setDelegate(new Delegate())
> dynamicClosure.call()
> Test.staticClosure.setDelegate(new Delegate()) 
> Test.staticClosure.call()
> ==================================================================
> Expected Output:
> delegate
> delegate
> delegate
> delegate
> Actual Output:
> NOT the delegate
> delegate
> delegate
> delegate



--
This message was sent by Atlassian Jira
(v8.20.1#820001)