i use maximumNumberOfParameters
to check number of parameters used in closure ;
def callerCls(Closure c){
prinltn 'Nb :'+c.maximumNumberOfParameters;
c();
}
the pb that the follwing snippet has the same result .
callerCls{
}
// and
callerCls{param1->
}
Both of them return:
Nb :1
Although the follwing code, returns Nb :2
.
callerCls{p1,p2->
}
How to distinguish closure where no parameter used (as the first snippet) ?
Answer had been found in groovy official site
Parameter notes A Closure without -> , ie {} , is a Closure with one argument that is implicitly named as 'it'. (see below for details) In some cases, you need to construct a Closure with zero arguments, eg using GString for templating, defining EMC Property etc. You have to explicity define your Closure as { -> } instead of just { }
You can also use varargs as parameters, refer to the Formal Guide for details. A JavaScript-style dynamic args could be simulated, refer to the Informal Guide.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.