groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Daniil Ovchinnikov (JIRA)" <j...@apache.org>
Subject [jira] [Comment Edited] (GROOVY-7956) Allow @DelegatesTo on named arguments
Date Thu, 09 Nov 2017 12:33:00 GMT

    [ https://issues.apache.org/jira/browse/GROOVY-7956?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15865747#comment-15865747
] 

Daniil Ovchinnikov edited comment on GROOVY-7956 at 11/9/17 12:32 PM:
----------------------------------------------------------------------

EDIT: I don't think this is the best solution, but I leave this comment for history.

I vote for: 
{code}@MapDescription(value = Point, required=['x'])
void methodDescribed(Map point) {
  println "$p.x, $p.y"
}

// with ability to annotate parameters
void methodDescribed2(someParameter, @MapDescription(Point) Map point) {
  println "$p.x, $p.y"
}
{code}
The second allows such calls to be checked: {{methodDescribed2(42, \[x: 1, y: 2])}}

If {{required}} is empty then all properties should be considered required.



was (Author: daniilo):
I vote for: 
{code}@MapDescription(value = Point, required=['x'])
void methodDescribed(Map point) {
  println "$p.x, $p.y"
}

// with ability to annotate parameters
void methodDescribed2(someParameter, @MapDescription(Point) Map point) {
  println "$p.x, $p.y"
}
{code}
The second allows such calls to be checked: {{methodDescribed2(42, \[x: 1, y: 2])}}

If {{required}} is empty then all properties should be considered required.


> Allow @DelegatesTo on named arguments
> -------------------------------------
>
>                 Key: GROOVY-7956
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7956
>             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
(v6.4.14#64029)

Mime
View raw message