Reputation: 93
Why does this groovy code
def versionString = '10.15.20'
int major
int minor
int patch
(major, minor, patch) = versionString.split(/\./).each { Integer.parseInt(it) }
println "$major.$minor.$patch"
throw this exception
Caught: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '10' with class 'java.lang.String' to class 'int'
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '10' with class 'java.lang.String' to class 'int'
instead of displaying 10.15.20?
I.e. why are the variables assigned with the output from split() rather than the output of the closure?
Upvotes: 0
Views: 550
Reputation: 171084
You need collect
(major, minor, patch) = versionString.split(/\./).collect { Integer.parseInt(it) }
Upvotes: 2