chengxing1993xc
chengxing1993xc

Reputation: 1

Groovy Syntax Confusion

I'm new to Groovy/Gradle and I'm currently learning the syntax, but I'm a bit confused by its syntax because it seems that same thing can be achieved by a lot of different ways. Does anyone know what's the meaning of the following groovy code:

something(x, y) { 
    // some code here 
}

It is definitely not a method declaration because it doesn't have the def keyword. It also doesn't look like an invocation of the something method, because that wouldn't explain the curly braces. What exactly does this code do?

Upvotes: 0

Views: 97

Answers (1)

Jeff Scott Brown
Jeff Scott Brown

Reputation: 27255

It is definitely not a method declaration because it doesn't have the def keyword.

Method definitions do not require the def keyword.

It also doesn't look like an invocation of the something method, because that wouldn't explain the curly braces. What exactly does this code do?

something(x, y) { 
    // some code here 
}

It is an invocation of a method. That code is invoking a method named something and passing 3 parameters. x and y are the first 2 parameters and a groovy.lang.Closure is the 3rd. That code is the same as the following:

def closure = {
    // some code here
}
something(x, y, closure)

Upvotes: 1

Related Questions