Common Groovy Errors

At my work we are using Groovy extensively and often mix Java syntax in Groovy. I want to post some common Groovy language errors that I think everybody in my team has committed at one point when we forget that Groovy is not Java. The first error that I will mention is that use of array initializers. In Java you can construct an array using an initializer like this:

String[] array = new String[]{"one", "two", "three"}

In Groovy, when we attempted to use this construct we would get a nasty and hard to understand compiler exception. Groovy is an expressive language and you can construct an initialized list in the following fashion:

def list = ["one", "two", "three"]   // ArrayList

The list variable is an instance of ArrayList. If you require an array you need to cast the list such as:

def arr = (String[])list   // String array

Now, this leads me to the second compiler error that I have encountered in Groovy. In Groovy I keep writing for loops the Java way:

for(int i; i < list.size(); i++) {
   System.out.println(list.get(i));
}

From my understanding Groovy only supports the for/in loop construct. If you need to iterate over a list using an index you could do the following:

for(i in 0 .. list.size()-1) {
   println list.get(i)
}

If you don’t need the index you can loop over an list in by using the each method provided by the Groovy Development Kit (GDK):

list.each {
   println it
}

Since I mention how to construct an initialized list in Groovy, let me also cover maps. In Groovy you can construct a map by using the following construct:

def map = [name:'Juixe', date:new Date()]

Since map is an instance of HashMap you can retrieve values using the get method but Groovy provides a different mechanism. In Groovy you can access a value from a map like this:

println map['name']   // returns Juixe

In the examples above I have mentioned how to create lists and maps with data. If you need to create a list or map without data you can use one of the following statements:

def emptyList = []
def emptyMap = [:]

A third error that I have commited is that I keep thinking of the each closures as for loops. I have received compilation erros because I try to continue or break out of a closure loop. Closures are code block so you can break out of them using the return keyword. I continue to the next element in a list do something like the following:

[1, 2, 3, 4, 5].each {
  if(it == 3)
	return
  println it
}

Technorati Tags: , , ,

Enjoy. Share. Be Happy.
  • Twitter
  • Facebook
  • StumbleUpon
  • del.icio.us
  • Tumblr
  • Google Bookmarks
  • FriendFeed
  • Yahoo! Buzz
  • Reddit
  • Digg
  • HackerNews
  • Suggest to Techmeme via Twitter
  • LinkedIn
  • Ping.fm
  • Identi.ca
  • Mixx
  • Furl

Related posts:

  1. Groovy Closures
  2. Gnarly Custom Groovy Closures
  3. Generic Java
  4. COM Scripting With Groovy
  5. Builder Pattern Made Groovy

This entry was posted in Java, TechKnow. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments

  1. Posted August 19, 2008 at 5:54 am | Permalink

    Thanks, this helped me figure out why I was getting a java.lang.ArrayStoreException passing a Groovy list to a method that took a String[] and a String … vararg.

  2. Posted September 17, 2008 at 1:41 am | Permalink

    If you want to iterate through a list with an index you can use

    list.eachWithIndex { it, i -> println “$i:$it” }

    For an early break out of an each, you would normally use find instead

    [1, 2, 3, 4, 5].find {
    println it
    it == 2
    }

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*