Groovy, Sometimes You Still Need a Semicolon.
Join the DZone community and get the full member experience.
Join For FreeExample the first: Generics at the end of a line:
def list = [1,2,3] as List<Integer> println list
If you try to compile this in Groovy it will give you the error message: 'unexpected token: println', however this:
def list = [1,2,3] as List<Integer>; println listGives the expected output.
Example the second: Ambiguous Closures
{-> assert GroovyClosureTest == owner.getClass() }() {-> assert GroovyClosureTest == delegate.getClass() }()
I don't think you'd really ever need to do something like this, but a closure can be defined and called on a single line. Because of Groovy's special closure parameter syntax (e.g. list.each() {} being synonomous with list.each({})) the compiler thinks I'm passing the second closure into the first as an argument. Again a semicolon is needed to seperate the two lines:
{-> assert GroovyClosureTest == owner.getClass() }(); {-> assert GroovyClosureTest == delegate.getClass() }()
From
Opinions expressed by DZone contributors are their own.
Comments