Thursday, August 21, 2008

Groovy;

The same three of us have been programming in groovy in ernest for the first time. We've each dinked around a bit at one time or another previously.

Overall I like it. Especially closures and the syntax for block closures when the receiver's last argument is a closure. The spectrum from programming nearly all-like-java on one end of groovy style, and programming nearly all-like-lisp-and-smalltalk at the other end of groovy style flows more smoothly than I initially gave groovy's designers credit for a few years ago.

One quirk - bit me for a dozen minutes or so, the oddity/convenience that groovy wants the keys of a map to be strings. What bit me was the expression...

{ foo: bar}
where foo and bar are variables.

The "convenience" groovy provides is the interpretation of foo as a string, i.e. "foo". The value for the pair is still the result of evaluating bar. So you end up with the pair (if bar is the integer 1)... "foo" : 1

If foo has the value 3 and you'd like the pair to be... 3 : 1 then you have to put foo in parentheses to force evaluation...

{ (foo): bar}
That's the closest thing to a true quirk I've encountered. But for parameter lists, etc. the convenience does pay off in neatness. The result is a very smalltalk-ish keyword message send, and combined with the block closure syntax, is a pleasant and familiar style for smalltalk-lovers. (e.g. coding a seaside-ish web framework using paul-graham-ish closures rather than continuations would be very fitting in groovy.)

Meanwhile the other affect groovy has had is that now when I edit java code, I'm forever not using semicolons at the end of statements.

Oh, and the tools still really suck.

5 comments:

  1. That's very Perly. In Perl the => is a synonym for "," designed to be used between keys and values in hashes; it converts any word to the left of it to a string.

    ReplyDelete
  2. Ed, but in Perl there's a meaningful difference between a => $b and $a => $b.

    ReplyDelete
  3. Yeah, in Perl, since "barewords" can't be ordinary variable names, you don't have that same jarring "why is it turning my variable into a string" effect. True.

    ReplyDelete
  4. groovy + maven made me nearly homicidal one late late night / early morning.

    in the end i gave up. i just never saw the value add. i stopped using "def" after a week so i could interop better with java, and after that it just looked like java to me.

    ReplyDelete
  5. "groovy + maven made me nearly homicidal"

    On the other hand, groovy + ant = gant, which is a really nice groovy-based build system.

    Simple, can run the ant tasks but uses groovy the markupbuilder for writing groovy build files, and you can program it in groovy.

    ReplyDelete