Groovy web console

subscribe to the feed Subscribe
to this
site

!!???

Published 3 weeks ago by MikeG
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
// based on https://stackoverflow.com/a/67182902/1015495
text = 'i am a ${interpolated} string'
interpolated = 'runtime'

engine = new groovy.text.SimpleTemplateEngine()
template = engine.createTemplate(text)

makeWithMap = template.make([interpolated : interpolated])
// these all work fine
println(makeWithMap)
println(makeWithMap.toString())
println((String) makeWithMap)

println("done with makeWithMap")

try {
    // uncomment this for a sane outcome
    // x = 1/0
    makeWithBindingVariables = template.make(this.binding.variables)
    // this will produce output, but stop executing after:
    println(makeWithBindingVariables)
    // these will NOT produce output, and will stop executing:
    println(makeWithBindingVariables.toString())
    println((String) makeWithBindingVariables)
} catch (Exception e) { // nothing is caught
    println("am dead ${e}")
} finally { // we never get here, either
    println("how did I break a finally block?")
}
// this won't be reached if any of the above are called
println("All done")