Package groovy.transform
Annotation Type NamedVariant
Allows construction of a named-arg equivalent method or constructor.
The method or constructor will have at least a first argument of type
Map and may have more arguments. As such, it can be called
using Groovy's named-arg syntax. The original method/constructor is retained
and is called by the generated method/constructor.
One benefit of this approach is the potential for improved type checking.
The annotated "tuple" method/constructor can be type rich and will be checked
as such during normal compilation. The generated method/constructor using
the map argument will be named-argument friendly but the map also hides
type information. The generated method however contains no business logic
so the chance of errors is minimal.
Any arguments identified as named arguments will be supplied as part of the map.
Any additional arguments are supplied in the normal tuple style.
Named parameters are identified in one of three ways:
- Use one or more
@NamedParamannotations to explicitly identify such parameters - Use one or more
@NamedDelegateannotations to explicitly identify such parameters as delegate parameters - If no parameters with
@NamedParamor@NamedDelegateannotations are found then:- If
autoDelegateis false (the default), all parameters are treated as if they were named parameters - If
autoDelegateis true, the first parameter is treated as if it is a delegate parameter
- If
@NamedParam and @NamedDelegate annotations.
Named arguments will be supplied via the map with their property name (configurable via
annotation attributes within @NamedParam) being the key and value being the argument value.
For named delegates, any properties of the delegate can become map keys.
Duplicate keys across delegate properties or named parameters are not allowed.
The type of delegate parameters must be compatible with Groovy's as cast operation from a Map.
Here is an example using implicit named parameters.
import groovy.transform.*
@NamedVariant
int makeSense(int dollars, int cents) {
100 * dollars + cents
}
assert makeSense(dollars: 2, cents: 50) == 250
Here is an example using a delegate parameter.
import groovy.transform.*You could also explicitly annotate the@ToString(includeNames=true)class Color { Integer r, g, b }@NamedVariantString foo(@NamedDelegateColor shade) { shade } def result = foo(g: 12, b: 42, r: 12) assert result.toString() == 'Color(r:12, g:12, b:42)'
shade argument with the @NamedDelegate annotation if you wanted.
The generated method will be something like this:
String foo(Map args) {
return foo(args as Color)
}
The generated method/constructor retains the visibility and return type of the original method/constructor
but the VisibilityOptions annotation can be added to customize the visibility. You could have the
annotated method/constructor private for instance but have the generated one be public.- Since:
- 2.5.0
- See Also:
-
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionbooleanIf true, add an implicit@NamedDelegateto the first parameter if no@NamedDelegateor@NamedParamannotations are found on any parameter.booleanIf true, will useasto convert map parameter to required classIf specified, must match the optional "id" attribute in an applicableVisibilityOptionsannotation.
-
Element Details
-
visibilityId
String visibilityIdIf specified, must match the optional "id" attribute in an applicableVisibilityOptionsannotation.- Default:
- "<DummyUndefinedMarkerString-DoNotUse>"
-
autoDelegate
boolean autoDelegateIf true, add an implicit@NamedDelegateto the first parameter if no@NamedDelegateor@NamedParamannotations are found on any parameter.- Since:
- 2.5.3
- Default:
- false
-
coerce
boolean coerceIf true, will useasto convert map parameter to required class- Since:
- 3.0.6
- Default:
- false
-