Groovy web console

subscribe to the feed Subscribe
to this
site
Groovy bug when creating Closure in loop - demo (via #groovywebconsole)
tweet this snippet Tweet
this
script

Groovy bug when creating Closure in loop - demo

Published 2 months ago by Mark Lagendijk
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
List<String> values = ['value1', 'value2']

// Works correctly, prints 'value1' and 'value2'
values.each { value -> println(value) }

// Doesn't work correctly, prints 'value2' and 'value2'
List<Closure> steps = values.collect { value -> return { println(value) } }
steps.each { step -> step() }

null