Gershom Maes
Gershom Maes

Reputation: 8150

Groovy Space-separated hashmap key

trying to answer a quick question and not having any luck searching online.

I'm learning Groovy and came across this code snippet online:

class Person {
    String name
    Person parent
    static belongsTo = [ supervisor: Person ]

    static mappedBy = [ supervisor: "none", parent: "none" ]

    static constraints = { supervisor nullable: true }
}

I'm specifically concerned with that last line of the Person class body. What does { supervisor nullable: true } mean? Are those multiple keys that both link to value true or something?

Thanks!

Upvotes: 1

Views: 298

Answers (1)

cfrick
cfrick

Reputation: 37033

This is short for:

static constraints = { 
    supervisor([nullable: true])
}

Which then means: define a class variable named constraints which holds a closure (closures are first class data in groovy). The closure (when called later) will execute the code within.

The code there is a DSL to configure the constraints for the later data base abstractions. So supervisor is a method call (the method does not exist, but the DSL's delegate takes care of that). The () maybe left out, if "unambiguous". Next if the method takes a Map as param, also the [] may be left out.

Note, that belongsTo and mappedBy are actual maps.

Upvotes: 4

Related Questions