Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
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
-
GROOVY-754
scoping
-
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.