Wednesday, January 21, 2015

groovy and ambiguity

I'm learning grails/groovy for work.

Grails is kind of cool, though I think its insistence on DRY (Don't Repeat Yourself) and "convention over configuration" lead to code that is easy to write, but harder to learn really well, because there's so much there the novice or intermediate coder might not know. Increasingly I'm aware of a bathtub-curve spectrum of coders, those who prefer power and conciseness, and those who prefer understandability and explicitness.

Anyway, a trivial JSON Controller in Grails:

package someapp

class SomeJsonController {

    def index() {
        def foo =
        render(contentType: "application/json") {
            ["key":"value", "a list":["1",2,foo]]

Put in the correct folder that gets automagically mapped so
{"key":"value","a list":["1",2,"3"]}

I was mildly bummed when I realized my observation that Groovy used JSON as its native list and map syntax was a bit off. It uses [] as delimiters for both, as opposed to JSON which uses [] for its lists and {} for its maps. So you see GWIM (Guess What I mean) magic at work in the code example.

I was curious what happened with the empty collection [] - how would it know what it was?  ... I ran the experiment with that Controller, and it's the empty list. Reading about groovy Collections I find out [] is canonically empty list, which makes sense, and [:] is the empty Map, which seems a little wrong to me - it feels more like a one member Map with null key and value, but I guess that ain't really a thing.

No comments:

Post a Comment