alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (ClosureInClosureBug.groovy)

This example Groovy source code file (ClosureInClosureBug.groovy) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

closureinclosurebug, closureinclosurebug, groovytestcase, groovytestcase

The Groovy ClosureInClosureBug.groovy source code

package groovy.bugs

/**
 * Bug illustrating the nested closures variable scope visibility issue.
 * l.each is ClosureInClosureBug$1 and it.each is ClosureInClosureBug$2
 * The variable text is not visible from ClosureInClosureBug$2.
 * Indeed, a closure can only see the variable defined outside this closure (one level up)
 * but cannot see what's in the second level.
 *
 * In order to make the test work, do not forget to uncomment the line "println(text)"
 *
 * @authour Guillaume Laforge
 */
class ClosureInClosureBug extends GroovyTestCase {

    void testInvisibleVariable() {
        def text = "test "

        def l = [1..11, 2..12, 3..13, 4..14]

        l.each {
            //println(text)
            it.each{
                println(text)
            }
        }
    }

    static void main(args) {
        def bug = new ClosureInClosureBug()
        bug.testInvisibleVariable()
    }
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy ClosureInClosureBug.groovy source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.