Friday, April 25, 2008

Printing Arrays in Groovy

While working through the arrays page of the Getting Started Guide at the Groovy codehaus site, I stumbled upon some interesting behaviour. I was adding "print" statements to the following block of code :
assert a as List == ['a', 2, 'c', false] 
so that I ended up with :

print
"\na as List : " + a as List

The result wasn't what I expected :
["\n", "a", " ", "a", "s", " ", "L", "i", "s", "t", " ", ":", " ", "{", "\"", "a", "\"", ",", " ", "2", ",", " ", "\"", "f", "o", "o", "\"", ",", " ", "f", "a", "l", "s", "e", "}"] I added parentheses around the "a as List" and got what I expected : a as List : ["a", 2, "foo", false] Simple lesson : pay attention to precedence! That or drink coffee first thing in the morning.

Also, turning the whole string into a list that easily is cool.
Blogged with the Flock Browser

No comments: