Groovy web console

subscribe to the feed Subscribe
to this
site

Loop var in closure (with copy))

Published 1 month ago by dnusbaum with tags closures
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def values = ["1", "2", "3"]
def closures = [:]
for (value in values) {
    println("Value in loop: " + value)
    def copiedValue = value
    closures[value] = { -> println("Value in closure: " + copiedValue) }
}
for (value in values) {
    closures[value]()
}