简体   繁体   中英

Groovy Closure generic return/parameters?

I have a method that should take a Closure with first two parameters Car and Tyre and should return a type of Vehicle.

Is there a way to declare the type of parameters in Groovy Closures?

I am thinking

method(Closure<Vehicle, Car, Tyre> closure); 

but what is the correct way?

You can only define the return type of a Closure, ie: Closure<Vehicle>

As of Groovy 2.3, you can use @ClosureParam to tweak the type system (see "Tweaking the type system" here ), but Groovy 2.3 is not currently in Grails I believe...

The generic parameter on Closure is used for the return type. Closure<String> shall return a string.

The parameters may be declared using the @ClosureParams annotation

http://beta.groovy-lang.org/docs/groovy-2.3.0-beta-1/html/gapi/groovy/transform/stc/ClosureParams.html

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM