Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8462967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:19:52+00:00 2026-06-10T14:19:52+00:00

I stumbled onto something with Groovy closures and delegates that I’m not sure is

  • 0

I stumbled onto something with Groovy closures and delegates that I’m not sure is an official part of the language or perhaps even a bug.

Basically I am defining a closure that I read in as a string from an external source,
and one of the variables in the class that defines the closure needs to be modified by the closure. I wrote
a simple example showing what I found works, and what does not work.

If you look at the test code below you will see a class that defines a variable

animal = "cat"

and two closures defined on the fly from strings that attempt to modify the animal variable.

This works >

String code = "{ ->   delegate.animal = 'bear';   return name + 'xx' ; }"

But this does not

String code = "{ ->   animal = 'bear';   return name + 'xx' ; }"

It seems like I need to explicitly qualify my to-be-modified variable with ‘delegate.’ for this to work.
(I guess i can also define a setter in the enclosing class for the closure to call to modify the value.)

So…. I’ve found out how to make this work, but I’d be interested if someone could point me to some groovy
doc that explains the rules behind this.

Specifically…. why will the simple assignment

animal = 'bear' 

affect the original variable ? Are there shadow copies being made here or something ?

import org.junit.Test

/*
 * Author: cbedford
 * Date: 8/30/12
 * Time: 1:16 PM
 */

class GroovyTest {
    String animal = "cat"
    String name = "fred"

    @Test
    public void testDelegateWithModificationOfDelegateVariable() {
    String code = "{ ->   delegate.animal = 'bear';   return name + 'xx' ; }"
    def shell = new GroovyShell()
    def closure = shell.evaluate(code)

    closure.delegate = this
    def result = closure()

    println "result is $result"
    println "animal is $animal"

    assert animal == 'bear'
    assert result == 'fredxx'
    }


    // This test will FAIL.
    @Test
    public void testDelegateWithFailedModificationOfDelegateVariable() {
    String code = "{ ->   animal = 'bear';   return name + 'xx' ; }"
    def shell = new GroovyShell()
    def closure = shell.evaluate(code)

    closure.delegate = this
    def result = closure()

    println "result is $result"
    println "animal is $animal"

    assert animal == 'bear'
    assert result == 'fredxx'
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:19:54+00:00Added an answer on June 10, 2026 at 2:19 pm

    Groovy closures have five strategies for resolving symbols inside closures:

    • OWNER_FIRST: the owner (where the closure is defined) is checked first, then the delegate
    • OWNER_ONLY: the owner is checked, the delegate is only checked if referenced explicitly
    • DELEGATE_FIRST: the delegate is checked first, then the owner
    • DELEGATE_ONLY: the delegate is checked first, the owner is only checked if referenced explicitly
    • TO_SELF: neither delegate nor owner are checked

    The default is OWNER_FIRST. Since the closure is defined dynamically, your owner is a Script object which has special rules itself. Writing animal = 'bear' in a Script will actually create a new binding called animal and assign 'bear' to it.

    You can fix your tests to work without explicitly referencing delegate by simply changing the resolve strategy on the closure before calling it with:

    closure.resolveStrategy = Closure.DELEGATE_FIRST
    

    This will avoid the odd the Script binding and use the delegate as expected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I stumbled across something that is not a problem, but something rather puzzling. I
I was looking through the docs and stumbled onto mysql_real_escape_string() and I'm not understanding
Ever stumbled on a tutorial that you feel is of great value but not
Am stumbled - went through docs, tutorials, etc, and am not sure what I
I think I may have stumbled onto a limitation of Flot, but I'm not
I've stumbled onto a very strange bug. Read the comments in the code to
while looking at some code I stumbled onto: throw /*-->*/new std::exception (//... and I
Recently stumbled upon a stock video footage site http://www.pond5.com/stock-video-footage/1/*.html , and saw that they
I stumbled over some code that appears to be correct. It is supposed to
While l was looking over some questions about MEF, I stumbled onto this particular

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.