Cool Java guy מוחמד
Cool Java guy מוחמד

Reputation: 1727

Groovy Optional parameter omit center value

I have a method in groovy. This method has second parameter as param?. I want to omit this parameter and pass only first and third parameter. Is there any way to do it.

Upvotes: 1

Views: 537

Answers (1)

tim_yates
tim_yates

Reputation: 171054

Not without changing your class

Why not add a new method that only takes 2 parameters?

Or move the optional parameter to the end and give it a default value?

Or you could define a method that returns a closure that only takes 2 params (and sticks a default value in for the middle parameter)

String threeParams(String a, int b, String c) {
    "$a:$b:$c"
}

Closure midCurry(int b) {
    { String a, String c -> threeParams(a, b, c) }
}

def twoParams = midCurry(1)

assert twoParams('tim', 'yates') == 'tim:1:yates'

Upvotes: 5

Related Questions