You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@groovy.apache.org by Leonard Brünings <gr...@bruenings-it.net> on 2021/08/04 20:00:20 UTC

@DelegatesTo for Closures used in annotations

It is possible to define Closures for annotations, e.g., Spock's
`@Requires({ os.windows })`.
However, it is currently impossible to tell the IDE what this annotation
will delegate to, as @DelegatesTo is only applicable for parameters.

Could we either allow @DelegatesTo for TYPE_USE or mabe add a new
dedicated annotation for that.

public @interface Requires {
  Class<DelegatesTo(PreconditionContext.class) ? extends Closure> value();
}

Alternatively, we could add METHOD as target, to the annotation.

public @interface Requires {
  DelegatesTo(PreconditionContext.class)
  Class<? extends Closure> value();
}

Cheers Leonard


Re: @DelegatesTo for Closures used in annotations

Posted by Daniil Ovchinnikov <da...@jetbrains.com>.
Since the delegation is effectively a part of a closure type, I’d expect @DelegatesTo to have TYPE_USE target.

Of course, it’s now tailored for parameters, so it probably should be a different annotation.
`genericTypeIndex` and `target` doesn’t make sense in TYPE_USE, so we are left with `value`, `strategy`, and `type`. 
I don’t believe it’s possible to define an owner of such closure reasonably, because it’s kind of dangling around, and without an owner the `strategy` doesn’t make sense. Now, `value` and `type` are essentially the same, but `type` supports generics. I don’t have a use case for `type`, but `value` could be covered by the following, if Groovy would support something like this:

```
@interface Requires {
  Class<? extends Precondition> value()
}

abstract class Precondition {
  def os = …
  abstract boolean checkRequirement() // SAM target
}

@Requires({ os.windows }) // or @Requires { os.windows } could be supported
class X {}

// which should be compiled to
@Requires(X$1)
class X {

  static class 1 extends Precondition {
    boolean checkRequirement() {
      os.windows
    }
  }
}
```

And with this approach there is no need for an annotation.

—

Daniil Ovchinnikov
JetBrains

> On 4 Aug 2021, at 22:00, Leonard Brünings <gr...@bruenings-it.net> wrote:
> 
> It is possible to define Closures for annotations, e.g., Spock's
> `@Requires({ os.windows })`.
> However, it is currently impossible to tell the IDE what this annotation
> will delegate to, as @DelegatesTo is only applicable for parameters.
> 
> Could we either allow @DelegatesTo for TYPE_USE or mabe add a new
> dedicated annotation for that.
> 
> public @interface Requires {
>   Class<DelegatesTo(PreconditionContext.class) ? extends Closure> value();
> }
> 
> Alternatively, we could add METHOD as target, to the annotation.
> 
> public @interface Requires {
>   DelegatesTo(PreconditionContext.class)
>   Class<? extends Closure> value();
> }
> 
> Cheers Leonard
> 


Re: @DelegatesTo for Closures used in annotations

Posted by Paul King <pa...@asert.com.au>.
Thanks!

On Sat, Aug 7, 2021 at 2:36 AM Marcin Zajączkowski <ms...@wp.pl> wrote:

> On 2021-08-05 14:39, Paul King wrote:
> > Marcin, I know we didn't progress, but do you recall whether we created
> an
> > issue last time?
>
> Good point Paul :-/. I should created a ticket to be able to track and
> and be visible in grooming.
>
> I've just created one for further consideration:
> https://issues.apache.org/jira/browse/GROOVY-10192
>
>
> Marcin
>
>
> >
> > On Thu, Aug 5, 2021 at 10:08 PM Marcin Zajączkowski <ms...@wp.pl>
> wrote:
> >
> >> I've been asking about the same thing back in 2020 with some proposals,
> >> but unfortunately without any reply:
> >>
> >>
> https://mail-archives.apache.org/mod_mbox/groovy-users/202001.mbox/%3cb62eefce-5d71-f394-8024-f2dd93c12c04@wp.pl%3e
> >>
> >> Nevertheless, maybe this time something constructive will be determined.
> >>
> >> Marcin
> >>
> >>
> >> On 2021-08-04 22:00, Leonard Brünings wrote:
> >>> It is possible to define Closures for annotations, e.g., Spock's
> >>> `@Requires({ os.windows })`.
> >>> However, it is currently impossible to tell the IDE what this
> annotation
> >>> will delegate to, as @DelegatesTo is only applicable for parameters.
> >>>
> >>> Could we either allow @DelegatesTo for TYPE_USE or mabe add a new
> >>> dedicated annotation for that.
> >>>
> >>> public @interface Requires {
> >>>   Class<DelegatesTo(PreconditionContext.class) ? extends Closure>
> >> value();
> >>> }
> >>>
> >>> Alternatively, we could add METHOD as target, to the annotation.
> >>>
> >>> public @interface Requires {
> >>>   DelegatesTo(PreconditionContext.class)
> >>>   Class<? extends Closure> value();
> >>> }
> >>>
> >>> Cheers Leonard
> >>>
> >>
> >>
> >> --
> >> https://blog.solidsoft.pl/ - Working code is not enough
> >>
> >
>
>
> --
> https://blog.solidsoft.pl/ - Working code is not enough
>
>

Re: @DelegatesTo for Closures used in annotations

Posted by Marcin Zajączkowski <ms...@wp.pl>.
On 2021-08-05 14:39, Paul King wrote:
> Marcin, I know we didn't progress, but do you recall whether we created an
> issue last time?

Good point Paul :-/. I should created a ticket to be able to track and
and be visible in grooming.

I've just created one for further consideration:
https://issues.apache.org/jira/browse/GROOVY-10192


Marcin


> 
> On Thu, Aug 5, 2021 at 10:08 PM Marcin Zajączkowski <ms...@wp.pl> wrote:
> 
>> I've been asking about the same thing back in 2020 with some proposals,
>> but unfortunately without any reply:
>>
>> https://mail-archives.apache.org/mod_mbox/groovy-users/202001.mbox/%3cb62eefce-5d71-f394-8024-f2dd93c12c04@wp.pl%3e
>>
>> Nevertheless, maybe this time something constructive will be determined.
>>
>> Marcin
>>
>>
>> On 2021-08-04 22:00, Leonard Brünings wrote:
>>> It is possible to define Closures for annotations, e.g., Spock's
>>> `@Requires({ os.windows })`.
>>> However, it is currently impossible to tell the IDE what this annotation
>>> will delegate to, as @DelegatesTo is only applicable for parameters.
>>>
>>> Could we either allow @DelegatesTo for TYPE_USE or mabe add a new
>>> dedicated annotation for that.
>>>
>>> public @interface Requires {
>>>   Class<DelegatesTo(PreconditionContext.class) ? extends Closure>
>> value();
>>> }
>>>
>>> Alternatively, we could add METHOD as target, to the annotation.
>>>
>>> public @interface Requires {
>>>   DelegatesTo(PreconditionContext.class)
>>>   Class<? extends Closure> value();
>>> }
>>>
>>> Cheers Leonard
>>>
>>
>>
>> --
>> https://blog.solidsoft.pl/ - Working code is not enough
>>
> 


-- 
https://blog.solidsoft.pl/ - Working code is not enough


Re: @DelegatesTo for Closures used in annotations

Posted by Paul King <pa...@asert.com.au>.
Marcin, I know we didn't progress, but do you recall whether we created an
issue last time?

On Thu, Aug 5, 2021 at 10:08 PM Marcin Zajączkowski <ms...@wp.pl> wrote:

> I've been asking about the same thing back in 2020 with some proposals,
> but unfortunately without any reply:
>
> https://mail-archives.apache.org/mod_mbox/groovy-users/202001.mbox/%3cb62eefce-5d71-f394-8024-f2dd93c12c04@wp.pl%3e
>
> Nevertheless, maybe this time something constructive will be determined.
>
> Marcin
>
>
> On 2021-08-04 22:00, Leonard Brünings wrote:
> > It is possible to define Closures for annotations, e.g., Spock's
> > `@Requires({ os.windows })`.
> > However, it is currently impossible to tell the IDE what this annotation
> > will delegate to, as @DelegatesTo is only applicable for parameters.
> >
> > Could we either allow @DelegatesTo for TYPE_USE or mabe add a new
> > dedicated annotation for that.
> >
> > public @interface Requires {
> >   Class<DelegatesTo(PreconditionContext.class) ? extends Closure>
> value();
> > }
> >
> > Alternatively, we could add METHOD as target, to the annotation.
> >
> > public @interface Requires {
> >   DelegatesTo(PreconditionContext.class)
> >   Class<? extends Closure> value();
> > }
> >
> > Cheers Leonard
> >
>
>
> --
> https://blog.solidsoft.pl/ - Working code is not enough
>

Re: @DelegatesTo for Closures used in annotations

Posted by Marcin Zajączkowski <ms...@wp.pl>.
I've been asking about the same thing back in 2020 with some proposals,
but unfortunately without any reply:
https://mail-archives.apache.org/mod_mbox/groovy-users/202001.mbox/%3cb62eefce-5d71-f394-8024-f2dd93c12c04@wp.pl%3e

Nevertheless, maybe this time something constructive will be determined.

Marcin


On 2021-08-04 22:00, Leonard Brünings wrote:
> It is possible to define Closures for annotations, e.g., Spock's
> `@Requires({ os.windows })`.
> However, it is currently impossible to tell the IDE what this annotation
> will delegate to, as @DelegatesTo is only applicable for parameters.
> 
> Could we either allow @DelegatesTo for TYPE_USE or mabe add a new
> dedicated annotation for that.
> 
> public @interface Requires {
>   Class<DelegatesTo(PreconditionContext.class) ? extends Closure> value();
> }
> 
> Alternatively, we could add METHOD as target, to the annotation.
> 
> public @interface Requires {
>   DelegatesTo(PreconditionContext.class)
>   Class<? extends Closure> value();
> }
> 
> Cheers Leonard
> 


-- 
https://blog.solidsoft.pl/ - Working code is not enough