Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!

Scott is a co-founder and Senior Software Architect at Near Infinity Corporation, an enterprise software development, training, and consulting services company based in Reston, Virginia. He has been developing enterprise and web applications for over 15 years professionally, and has developed applications using Java, Ruby/Rails, Groovy/Grails and Python. His main areas of interest include object-oriented design, system architecture, testing, and frameworks of all types including Spring, Hibernate, Ruby on Rails, Grails, and Django. In addition, Scott enjoys learning new languages to make himself a better and more well-rounded developer a la The Pragmatic Programmers' advice to "learn one language per year." Scott is a DZone MVB and is not an employee of DZone and has posted 21 posts at DZone. You can read more from them at their website. View Full User Profile

Groovy, Sometimes You Still Need a Semicolon.

11.06.2009
Email
Views: 3220
  • submit to reddit
Like Javascript, semicolons are optional in Groovy except for when they aren't optional. These examples are both pretty contrived, though I found both because they're actually something that I've written, and could both be written better. That's not really the point I'm making though. When something doesn't compile when it looks like it clearly should sometimes it's hard to track down why, and it's surprising to learn that it's because you need a semicolon.

Example 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 list
Gives 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

Tags:
Published at DZone with permission of Scott Leberknight, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Matthew Passell replied on Fri, 2009/11/06 - 12:10pm

I'm not sure about the second one, but the first might just be a compiler bug.  Have you asked the Groovy team?

--Matt
The Software Grove

Octavian Covalschi replied on Sun, 2009/11/08 - 7:56pm

What version are you using? I've just tried 1st example with 1.6.4 and it compiles and runs fine.

class App {
public static void main(String[] args) {
def list = [1,3,4,5,6] as List<Integer>
println list
}
}

I've tried both in Eclipse and groovyConosole.

Thanks.

John Cato replied on Mon, 2009/11/09 - 11:35am in response to: octix

The generics problem doesn't compile in 1.5.5 (which we're stuck on at the moment because of a Groovy/JAXB problem) so it looks like that one has been resolved.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.