Showing posts with label Closure. Show all posts
Showing posts with label Closure. Show all posts

Thursday, August 11, 2016

Groovy call a closure with parameters

Closure closureTest = { def args1 = null, def args2 = null ->
    /* do something you wish */
}

/* Call above closure as like: */
closureTest.call() /* It will pass args1 & args2 both null */
closureTest("value_args1") /* It will pass args2 null */
closureTest(null, "value_args2") /* It will pass args1 null */
closureTest("value_args1", "value_args2") /* It will pass both args1 & args2 value */

/* You can check number of parameters accept by a closure as like: */
closureTest.maximumNumberOfParameters