audience, you first make a fool out of yourself and then learn.
That?s what I did at my Get Groovier with Grails talk in Salt Lake City on Saturday. I was nearing
completion of my mostly live coding talk when I ran into a problem. No matter what I did, the page would
not update after I threw in some AJAX together. When you mess up in front of good audience,
everyone eagerly tries to help. Well, after quite some logical thinking, I got a suspicion and nailed down
the problem. Here is an over simplification of what I ran into:
Consider the following Ruby code (I got to Groovy after learning Ruby, so my mind thinks about Ruby quite a bit
while writing Groovy):
def foo(val)
if val > 4
"haha"
else
"hoho"
end
end
puts foo(5)
The output is ?haha.? In Ruby the last expression evaluated is returned by convention from the method.
Same is in Groovy. But, here is similar Groovy code:
def foo(val)
{
if (val > 4)
{
"haha"
}
else
{
"hoho"
}
}
println foo(5)
The output is null. I guess it assumes the last statement is the empty statement before the last }. I need
to look at the language specification to see what it says. After much ado, I put a return before ?haha? and
?hoho? and got it working (In the actual example I was playing with, had to put return before the
result hash table). Of course, I will not forget this anymore.