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.

Source de l’article sur DZONE

L’assistance proposée par ANKAA PMO

ANKAA PMO présent depuis plus de 20 ans sur le marché des services IT, accompagne les DSI dans leur recherche de compétences pour des besoins de renforts en mode régie ou l’externalisation de projets.
Vous souhaitez plus d’information ? Cliquez ici


0 réponses

Laisser un commentaire

Participez-vous à la discussion?
N'hésitez pas à contribuer!

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *