groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Paul King (JIRA)" <>
Subject [jira] [Commented] (GROOVY-7956) Allow @DelegatesTo on named arguments
Date Thu, 06 Oct 2016 21:42:21 GMT


Paul King commented on GROOVY-7956:

That is essentially what Sergei's transform does now - it just annotates the method assuming
the delegate is the first argument rather than annotating the delegate (as you suggest and
I probably prefer).

Sergei's version also has specific restrictions on the other parameters but we don't need
to limit ourselves in the same way. Currently, this:
def animal(Animal animal) {
    // process animal

class Animal {
    String type
    String name

assert animal(type: 'Dog', name: 'Rover') == 'DOG:Rover'
adds this extra method:
@MapArguments(typeHint = Animal)
    public Object animal(Map __namedArgs) {
        return this.animal((( __namedArgs ) as Animal))
The advantage of annotating the method is that it is slightly easier to write the transform
but there is no reason why the generated method couldn't use a different annotation on the
generated Map parameter.

> Allow @DelegatesTo on named arguments
> -------------------------------------
>                 Key: GROOVY-7956
>                 URL:
>             Project: Groovy
>          Issue Type: New Feature
>          Components: GEP
>            Reporter: Graeme Rocher
> In order to aid static compilation for builders we have {{@DelegatesTo}} which allows
statically compiled code to know what the delegate of a closure is.
> This proposal is to allow {{@DelegatesTo}} on {{Map}} types such that IDEs and the static
compiler can resolve the target type the named arguments are to be used on.
> For example:
> {code}
> class Farm {
>      void animal(@DelegatesTo(Animal) Map arguments, @DelegatesTo(AnimalBuilder) Closure
callable) {
>              def animal = new Animal(arguments)
>              // handle closure
>     }
> } 
> class Animal { String name }
> {code}
> The following code would then fail to compile :
> {code}
> def farm = new Farm()
> // compilation failure, no name property on Animal
> farm.animal(nam: "Dog")  { 
> }
> {code}
> It would then be down to IDEs to also provide support for code completion etc.

This message was sent by Atlassian JIRA

View raw message