groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dinko Srkoč <dinko.sr...@gmail.com>
Subject Re: Get delegate in an ExtensionModule
Date Fri, 04 Dec 2015 13:25:34 GMT
On 4 December 2015 at 13:38, Arnaud Esteve <arnaud.esteve@gmail.com> wrote:
> Ok, I'll maybe try to ;)
>
> Another question in regard of ExtensionModules, is it possible to make a
> class comparable ?
>
> I tried to implement compareTo (in a static way, within the extension
> module) but it doesn't seem to work. And actually, it makes sense, because
> the classe wasn't comparable.

I haven't tried it, but it probably does, meaning
`car1.compareTo(car2)` would probably work.

On the other hand, since class `Car` didn't implement `Comparable`
(and is not "comparable" in that sense), `car1 <=> car2` wouldn't
work.

>
>
> Say I have a Car object, how should I write the compareTo method ?
>
> class CarExtension {
>   static int compareTo(Car self, Car other) { // or Object other ? or ? :[
>     self.name.compareTo(other.name) // should this work ?
>
>   }
> }

This looks right, but see my observation about comparable above.

Cheers,
Dinko

>
> Le 04/12/2015 12:00, Dinko Srkoč a écrit :
>>
>> On 4 December 2015 at 11:41, Arnaud Estève <arnaud.esteve@gmail.com>
>> wrote:
>>>
>>> [...]
>>> Just a question (pure curiosity) though, what happens when many jars
>>> register many extension modules on the same type/method, and you embed
>>> both
>>> the jars ?
>>
>> I would assume one would win and shadow the other. Sounds like a nice
>> experiment. ;-)
>>
>> Cheers,
>> Dinko
>>
>>> On Fri, Dec 4, 2015 at 11:18 AM, Dinko Srkoč <dinko.srkoc@gmail.com>
>>> wrote:
>>>>
>>>> Just forward the call to the original `asType` method:
>>>>
>>>>    org.codehaus.groovy.runtime.StringGroovyMethods.asType(self,
>>>> someClass)
>>>>
>>>> Cheers,
>>>> Dinko
>>>>
>>>> On 4 December 2015 at 10:16, Arnaud Estève <arnaud.esteve@gmail.com>
>>>> wrote:
>>>>>
>>>>> Hello,
>>>>>
>>>>> I'm currently playing with extension modules and congrats, it's very
>>>>> fun
>>>>> to
>>>>> add syntaxic sugar to an existing API.
>>>>>
>>>>> I was wondering if it was possible to delegate to the "super" method
>>>>> within
>>>>> an extension module.
>>>>>
>>>>> For instance, let's imagine I want to overload String's asType() method
>>>>> :
>>>>>
>>>>> class MyStringExtension {
>>>>>    static Object asType(String self, Class someClass) {
>>>>>      if (myClass = SomethingICanHandler.class) {
>>>>>        // create an object from my string
>>>>>      } else {
>>>>>        // Here I'd like to "delegate" to the default String asType()
>>>>> method
>>>>>      }
>>>>>    }
>>>>> }
>>>>>
>>>>> With metaclass it would be easy, but is it even possible (and does it
>>>>> make
>>>>> sense ?) to do so with an extension module ?
>>>>>
>>>>> Thanks a lot :)
>>>>>
>>>
>

Mime
View raw message