Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-4056

Scope problem with anonymous classes

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • None
    • None
    • Compiler
    • None
    • Ubuntu Karmic , OpenJDK , Groovy Trunk 2010-02-14 11:28+00:00

    Description

      In the code fragment:

        for ( int i = 0 ; i < numberOfTasks ; ++i ) {
          final int taskId = i
          processes[taskId] = new CSProcess ( ) {
            public void run ( ) {
              double sum = 0.0 ;
              final long start = 1 + taskId * sliceSize
              final long end = ( taskId + 1 ) * sliceSize
              for ( long j = start ; j <= end ; ++j ) {
                final double x = ( j - 0.5d ) * delta
                sum += 1.0d / ( 1.0d + x * x )
              }
              channels[taskId].out ( ).write ( sum )
            }
          }
        }
      

      The outer loop uses i and in Java the loop in the run method uses i as well since there is no scope conflict. In Groovy, as noted above you cannot use i in the run method of the anonymous class as it is reported as a scoping violation.

      Attachments

        Activity

          People

            Unassigned Unassigned
            russel Dr. Russel Winder
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: