groovy-notifications mailing list archives

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


Graeme Rocher commented on GROOVY-7956:

What I don't like so much about {{@Properties([ x: Integer, y: Integer ])}} is that you can
see that quickly turning into a complex annotation that is hard to read and IMO annotations
should always be as simple as possible. In contrast {{@DelegatesTo(Animal}}} allows you to
specify the properties and their types in a more natural place. ie in a class definition (in
this case {{Animal}}). If we then want to add a way to make it possible to specify required
args that could be easily doable:

class Animal {
      String specified

And that feels much more natural than trying to specify everything in an annotation on the
argument to the method like {{@Properties([ x: Integer, y: Integer ])}}. Granted maybe you
do as a secondary requirement want the ability to not always bind to a given type, but I don't
understand the use case for that personally.

> 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