Tuesday, December 29, 2009

Grails Gotcha #1

Whenever you work in Grails and wonder why something like this is not working:



Check the types of objects you're trying to compare. Most likely, one of the following gotchas is keeping you busy.

1. Trying to check for containment of Strings. In case you're using Groovy's support for embedding variables in a string (using "foo $bar" notation), the resulting object is of type GString not String.

2. Trying to check for containment of byte-code-modified objects. Especially when working in Hibernate-environments, objects might not really be what you think they are. They are probably of a type like "MyClass__javaassited_36" where the objects in your list are of type "MyClass".

You might be able to change your statement to use the convenient "any { it -> // code }" method included in the Groovy JDK (Object class).

Hope it saves you from some of the headaches it's been giving me!

No comments:

Post a Comment