Hello there,
> On 25. 10. 2016, at 11:40 PM, David Karr <[email protected]> wrote:
> On Tue, Oct 25, 2016 at 1:34 PM, Dinko Srkoč <[email protected]> wrote:
>> On 25 October 2016 at 16:02, VG <[email protected]> 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