Groovy supports named arguments for methods. In fact, Groovy collects all named arguments (defined using the name followed by a :
and the value) into a Map
. The Map
must be the first parameter of the method to make it all work. Since Groovy 2.5.0, we can use the @NamedVariant
AST transformation annotation to let Groovy create a method where the first parameter is a Map
that supports named arguments for an existing method. The existing method is still available, but Groovy adds an extra method to our generated class.
By default, Groovy will make the first parameters of the original method part of the new method for supporting named arguments. If the first parameters are a class type, then the properties of the class can be used as named arguments. We can also explicitly define which parameters of our original method should be named arguments using the annotations @NamedParam
and @NamedDelegate
. These annotations need to be defined for each parameter. The newly created method by the AST transformation invokes the original method.
Laisser un commentaire
Participez-vous à la discussion?N'hésitez pas à contribuer!