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-365

variables created within the closure fail to be visible outside as local variables

  • Log In
  • Views
    • XML
    • Word
    • Printable

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Cannot Reproduce
  • Affects Version/s: 1.0-beta-4
  • Fix Version/s: 1.0-JSR-4
  • Component/s: None
  • Labels:
    None
  • Environment:
    Windows XP Professional

Description

The following code from the User Guide should work:

count = 0
[1, 2, 3, 4].each { count += it; last = it }
println("the sum is ${count} and the last item was ${last}")


but gives:

[]
[]
Caught: java.lang.NullPointerException
java.lang.NullPointerException
at test$1.doCall(test.groovy:2)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.codehaus.groovy.runtime.ReflectionMetaMethod.invoke(ReflectionMet
aMethod.java:56)
at groovy.lang.Closure.call(Closure.java:195)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMe
thods.java:283)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.codehaus.groovy.runtime.ReflectionMetaMethod.invoke(ReflectionMet
aMethod.java:56)
at org.codehaus.groovy.runtime.NewStaticMetaMethod.invoke(NewStaticMetaM
ethod.java:100)
at groovy.lang.MetaClass.doMethodInvoke(MetaClass.java:846)
at groovy.lang.MetaClass.invokeMethod(MetaClass.java:268)
at org.codehaus.groovy.runtime.Invoker.invokeMethod(Invoker.java:130)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.
java:101)
at test.run(test.groovy:2)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.codehaus.groovy.runtime.ReflectionMetaMethod.invoke(ReflectionMet
aMethod.java:56)
at groovy.lang.MetaClass.doMethodInvoke(MetaClass.java:846)
at groovy.lang.MetaClass.invokeMethod(MetaClass.java:268)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.codehaus.groovy.runtime.ReflectionMetaMethod.invoke(ReflectionMet
aMethod.java:56)
at groovy.lang.MetaClass.doMethodInvoke(MetaClass.java:846)
at groovy.lang.MetaClass.invokeMethod(MetaClass.java:268)
at org.codehaus.groovy.runtime.Invoker.invokeMethod(Invoker.java:130)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.
java:101)
at test.invokeMethod(test.groovy)
at org.codehaus.groovy.runtime.Invoker.invokeMethod(Invoker.java:117)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.
java:101)
at org.codehaus.groovy.runtime.InvokerHelper.runScript(InvokerHelper.jav
a:332)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.codehaus.groovy.runtime.ReflectionMetaMethod.invoke(ReflectionMet
aMethod.java:56)
at groovy.lang.MetaClass.doMethodInvoke(MetaClass.java:846)
at groovy.lang.MetaClass.invokeStaticMethod(MetaClass.java:329)
at org.codehaus.groovy.runtime.Invoker.invokeMethod(Invoker.java:124)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.
java:101)
at test.main(test.groovy)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.codehaus.groovy.runtime.ReflectionMetaMethod.invoke(ReflectionMet
aMethod.java:56)
at groovy.lang.MetaClass.doMethodInvoke(MetaClass.java:846)
at groovy.lang.MetaClass.invokeStaticMethod(MetaClass.java:329)
at org.codehaus.groovy.runtime.Invoker.invokeMethod(Invoker.java:124)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.
java:101)
at groovy.lang.GroovyShell.run(GroovyShell.java:183)
at groovy.lang.GroovyShell.main(GroovyShell.java:88)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.codehaus.classworlds.Launcher.launchStandard(Launcher.java:410)
at org.codehaus.classworlds.Launcher.launch(Launcher.java:344)
at org.codehaus.classworlds.Launcher.main(Launcher.java:474)


If we include the initialization of 'last' everything works:

count = 0
last = 0
[1, 2, 3, 4].each { count += it; last = it }
println("the sum is ${count} and the last item was ${last}")

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
John Stump added a comment - 08/Jul/04 11:47 AM

I know that this has been discussed as a feature of closures, but I wonder if we really want this feature? To me, one nifty thing about closures is that they have their own state, meaning they can create their own variables, which are retained between calls to the same closure object. It seems to me that this behavior would be removed if we supported the feature this bug refers to.

Show
John Stump added a comment - 08/Jul/04 11:47 AM I know that this has been discussed as a feature of closures, but I wonder if we really want this feature? To me, one nifty thing about closures is that they have their own state, meaning they can create their own variables, which are retained between calls to the same closure object. It seems to me that this behavior would be removed if we supported the feature this bug refers to.
Hide
Permalink
Guillaume Laforge added a comment - 22/Aug/05 4:06 AM

I couldn't reproduce this bug in 1.0-JSR-03.

Show
Guillaume Laforge added a comment - 22/Aug/05 4:06 AM I couldn't reproduce this bug in 1.0-JSR-03.

People

  • Assignee:
    Unassigned
    Reporter:
    Pedro Costa
Vote (0)
Watch (1)

Dates

  • Created:
    04/Apr/04 10:33 PM
    Updated:
    22/Aug/05 4:06 AM
    Resolved:
    22/Aug/05 4:06 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.