Take the following controller:
package test
class TestController {
static defaultAction = "test"
def test() {
render "test"
}
}
Why is test defined with def test() { instead of something like void test() {? Isn’t the def keyword only used for closures or functions in a script (i.e. not in a Groovy class)?
Burt’s answer is correct but the real problem I was having is that I misunderstood what
defis. Rather than being likevarin JavaScript, you can think of it likeObjectin Java.I thought that using
defwas like doing (JavaScript)while in reality it’s just like (Java)
It’s not a different kind of function/closure, it’s like a return type—
defcan be applied to any object (Groovy has no data primitives, so any value is also an object, unlike in Java).It helps my Java brain to think of
as