Samson
Samson

Reputation: 107

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?

Upvotes: 7

Views: 5156

Answers (2)

tim_yates
tim_yates

Reputation: 171084

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...

Upvotes: 8

Will
Will

Reputation: 14529

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

Upvotes: 0

Related Questions