You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by VG <vi...@gmail.com> on 2016/10/25 14:02:31 UTC

@Delegate and Date

Hi there,

why this does not work?

class Example {
    @Delegate Date when
}

def x = new Example(when: new Date())
x.next()  // fails

I checked
http://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/Date.html and
the next method is available there? Should not the method next be available
through @delegate?

Thanks, Vitor





--
View this message in context: http://groovy.329449.n5.nabble.com/Delegate-and-Date-tp5736295.html
Sent from the Groovy Users mailing list archive at Nabble.com.

Re: @Delegate and Date

Posted by VG <vi...@gmail.com>.
Hi, 

Thanks for explanation. I don't think it is 100% clear in the groovy
documentation, but I understood the issue.

Thank you. :)

Cheers,
VG



--
View this message in context: http://groovy.329449.n5.nabble.com/Delegate-and-Date-tp5736295p5736333.html
Sent from the Groovy Users mailing list archive at Nabble.com.

Re: @Delegate and Date

Posted by Jochen Theodorou <bl...@gmx.org>.
On 26.10.2016 00:08, ocs@ocs.cz wrote:
[...]
>> And just to interject the question the OP is probably going to ask:
>> Why should it matter whether it's an "extension" or not?  Is it
>> documented that the methods available on a Delegate exclude what
>> Groovy has added to the class?
>
> Jochen or someone other probably could give a better explanation, but myself, I would dare a guess that the problem is that @Delegate is not a true redirector (which I am still not quite sure is even possible to write Java-side!), but (essentially) a request for the compiler to add redirection stubs for all the known delegate methods.
>
> The trick is that Groovy extensions are *not* known methods: they are not part of the class API, instead, they are added metaclass-level. Thus, the @Delegate AST does not know of them, and does not add appropriate stubs.

exactly. The same applies to the usage of categories and to methods 
added to the meta class. The problem is reduced if we delegate to the 
implementation of an interface and all the important logic is based on 
the interface type. Because @Delegate per default implements the 
interfaces of the delegates.

> If you stayed completely at the dynamic side instead of using generated stubs through @Delegate, it would work; on the other hand, it would not work from Java code:
>
> ===
> class Example {
>     Date when
>     def methodMissing(String name, args) {
>        when."$name"(*args)
>     }
> }
> def x = new Example(when: new Date())
> println x.next()  // works
> ===

It is not an either-or. You can do both, causing less load for 
methodMissing and faster dispatch.

> Finally, as for the presumed question whether the @Delegate AST _could_ find the methods added through extensions... I am not entirely sure. I think it could, but it might get a bit difficult and perhaps even sort of unreliable. Definitely it would be problematic if called from Java.

Presuming the availability and usage of certain extension methods is 
imho ok for the static compiler, but not for dynamic Groovy - not if you 
expect a runtime added extension method to have influence

bye Jochen



Re: @Delegate and Date

Posted by "ocs@ocs.cz" <oc...@ocs.cz>.
Hello there,

> On 25. 10. 2016, at 11:40 PM, David Karr <da...@gmail.com> wrote:
> On Tue, Oct 25, 2016 at 1:34 PM, Dinko Srkoč <di...@gmail.com> wrote:
>> On 25 October 2016 at 16:02, VG <vi...@gmail.com> wrote:
>>> why this does not work?
>>> 
>>> class Example {
>>>    @Delegate Date when
>>> }
>>> 
>>> def x = new Example(when: new Date())
>>> x.next()  // fails
>>> 
>>> I checked
>>> http://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/Date.html and
>>> the next method is available there? Should not the method next be available
>>> through @delegate?
>> 
>> `next()` is not really a method on `java.util.Date`, but a Groovy's extension.
>> 
>> `Date`'s methods are all there. E.g.
>> 
>>    x.time // works
> 
> And just to interject the question the OP is probably going to ask:
> Why should it matter whether it's an "extension" or not?  Is it
> documented that the methods available on a Delegate exclude what
> Groovy has added to the class?

Jochen or someone other probably could give a better explanation, but myself, I would dare a guess that the problem is that @Delegate is not a true redirector (which I am still not quite sure is even possible to write Java-side!), but (essentially) a request for the compiler to add redirection stubs for all the known delegate methods.

The trick is that Groovy extensions are *not* known methods: they are not part of the class API, instead, they are added metaclass-level. Thus, the @Delegate AST does not know of them, and does not add appropriate stubs.

If you stayed completely at the dynamic side instead of using generated stubs through @Delegate, it would work; on the other hand, it would not work from Java code:

===
class Example {
   Date when
   def methodMissing(String name, args) {
      when."$name"(*args)
   }
}
def x = new Example(when: new Date())
println x.next()  // works
===

Finally, as for the presumed question whether the @Delegate AST _could_ find the methods added through extensions... I am not entirely sure. I think it could, but it might get a bit difficult and perhaps even sort of unreliable. Definitely it would be problematic if called from Java.

All the best,
OC


Re: @Delegate and Date

Posted by David Karr <da...@gmail.com>.
On Tue, Oct 25, 2016 at 1:34 PM, Dinko Srkoč <di...@gmail.com> wrote:
> On 25 October 2016 at 16:02, VG <vi...@gmail.com> wrote:
>> Hi there,
>>
>> why this does not work?
>>
>> class Example {
>>     @Delegate Date when
>> }
>>
>> def x = new Example(when: new Date())
>> x.next()  // fails
>>
>> I checked
>> http://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/Date.html and
>> the next method is available there? Should not the method next be available
>> through @delegate?
>
> `next()` is not really a method on `java.util.Date`, but a Groovy's extension.
>
> `Date`'s methods are all there. E.g.
>
>     x.time // works

And just to interject the question the OP is probably going to ask:
Why should it matter whether it's an "extension" or not?  Is it
documented that the methods available on a Delegate exclude what
Groovy has added to the class?

Re: @Delegate and Date

Posted by Dinko Srkoč <di...@gmail.com>.
On 25 October 2016 at 16:02, VG <vi...@gmail.com> wrote:
> Hi there,
>
> why this does not work?
>
> class Example {
>     @Delegate Date when
> }
>
> def x = new Example(when: new Date())
> x.next()  // fails
>
> I checked
> http://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/Date.html and
> the next method is available there? Should not the method next be available
> through @delegate?

`next()` is not really a method on `java.util.Date`, but a Groovy's extension.

`Date`'s methods are all there. E.g.

    x.time // works

Cheers,
Dinko

>
> Thanks, Vitor
>
>
>
>
>
> --
> View this message in context: http://groovy.329449.n5.nabble.com/Delegate-and-Date-tp5736295.html
> Sent from the Groovy Users mailing list archive at Nabble.com.