jira.codehaus.org

  • Log In Access more options
    • Online Help
    • Keyboard Shortcuts
    • About JIRA
    • JIRA Credits
    • What?s New
  • Dashboards Access more options (Alt+d)
  • Projects Access more options (Alt+p)
  • Issues Access more options (Alt+i)
  • groovy
  • GROOVY-673

incorrect scoping in closures and blocks

  • Log In
  • Views
    • XML
    • Word
    • Printable

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.0-beta-8
  • Fix Version/s: 1.0-JSR-2
  • Component/s: ast builder, lexer, parser
  • Labels:
    None

Description

i=5;
print i;
[1, 2, 3].each {
item |
int i=0;
i+=item;
print item;
print i;
};
print i

this example code should print 51122335 but prints 51122333. So the i in the closure is different from the i outside when inside the closure but leaving the closure the outer i will be overwritten with the inner i. As there are different scopes the outer i should still contain the old value.

Issue Links

is depended upon by

Task - A task that needs to be done. GROOVY-754 scoping

  • Major - Major loss of function.
  • Closed - The issue is considered finished, the resolution is correct. Issues which are not closed can be reopened.

Activity

Ascending order - Click to sort in descending order
  • All
  • Comments
  • Work Log
  • History
  • Activity
Hide
Permalink
blackdrag blackdrag added a comment - 08/Dec/04 5:52 AM

I've done one more test to see if it is different for blocks, so I tested the following code in the groovysh:

i=2
L: {
int i=4
print i
}
print i

this should print 42 but prints 44.

BTW, it's not allowed in Java to redifine a varibale in a block. Doing this would give a "i already defined in " message.

Show
blackdrag blackdrag added a comment - 08/Dec/04 5:52 AM I've done one more test to see if it is different for blocks, so I tested the following code in the groovysh: i=2 L: { int i=4 print i } print i this should print 42 but prints 44. BTW, it's not allowed in Java to redifine a varibale in a block. Doing this would give a "i already defined in " message.
Hide
Permalink
blackdrag blackdrag added a comment - 08/Dec/04 5:54 AM

changed to "...and blocks" because blocks are also an issue

Show
blackdrag blackdrag added a comment - 08/Dec/04 5:54 AM changed to "...and blocks" because blocks are also an issue
Hide
Permalink
blackdrag blackdrag added a comment - 14/Jun/05 6:05 AM

it's not allowed to declare a varibale of the same name inside a block/closure. The new scope checker should catch this case

Show
blackdrag blackdrag added a comment - 14/Jun/05 6:05 AM it's not allowed to declare a varibale of the same name inside a block/closure. The new scope checker should catch this case

People

  • Assignee:
    blackdrag blackdrag
    Reporter:
    blackdrag blackdrag
Vote (0)
Watch (0)

Dates

  • Created:
    07/Dec/04 4:00 PM
    Updated:
    14/Jun/05 6:05 AM
    Resolved:
    14/Jun/05 6:05 AM
  • Atlassian JIRA (v5.0.4#731-sha1:3aa7374)
  • Report a problem
  • Powered by a free Atlassian JIRA open source license for Codehaus. Try JIRA - bug tracking software for your team.