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

Groovy example source code file (ClosureTypedVariableBug.groovy)

This example Groovy source code file (ClosureTypedVariableBug.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

closure, closure, closuretypedvariablebug, closuretypedvariablebug, groovytestcase, groovytestcase, integer, integer

The Groovy ClosureTypedVariableBug.groovy source code

package groovy.bugs

/**
 * @version $Revision: 21312 $
 */
class ClosureTypedVariableBug extends GroovyTestCase {
    
    void testBug2() {
        def count = makeClosure(0)
        assert count == 1
        
        count = makeClosure2(0)
        assert count == 1
    }


    def makeClosure(Number count) {
        def closure = { count = it }
        closure(1)
        return count
    }

    def makeClosure2(Number c) {
        def count = c
        def closure = { count = it }
        closure(1)
        return count
    }

    void testBug() {
        Integer count = 0
        def closure = { count = it }
        closure(1)
        assert count == 1
    }
    
    void testBug3() {
        def closure = getElementClosure("p")
        def answer = closure("b")
        def value = answer("c")
        println "returned : ${value}"
    }
    
    Closure getElementClosure(tag) {
        return { body ->
            if (true) {
                return {"${body}"}
            }
            else {
                body = null
            }
        }
    }
    
    void testDoubleSlotReference() {
        // there was a bug that the local variable index
        // was wrong set for a closure shared variable. 
        // One slot should have be used and one was used sometimes
        // Thus resulting in sometimes assuming a wrong index 
        double d1 = 1.0d
        double d2 = 10.0d
        1.times { d1=d1*d2 }
        assert d1==10d
        
        long l1 = 1l
        long l2 = 10l
        1.times { l1=l1*l2 }
        assert l1==10l
    }
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy ClosureTypedVariableBug.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.