[groovy-user] operator overloading

classic Classic list List threaded Threaded
22 messages Options
123
Reply | Threaded
Open this post in threaded view
|

[groovy-user] operator overloading

John Sanabria
Thanks for your help, i have another question

i read in, http://groovy.codehaus.org/Operator+Overloading, that i can
overload the operator, e.g. +, implementing the method plus inside a class
that i want to overload the operator. Sample from tutorial (Closure).

Assume the next .groovy file:

-------------------- gvector.groovy -----------------------
public class GVector extends Vector {
        public GVector() {
                super();
        }

        public void apply(c) {
                for (i in 0..<size()) {
                        this[i] = c(this[i]);
                }
        }
        public GVector plus(b) {
                GVector result
                for (i in 0..<size())
                  result[i] = this[i] + b[i]
                return result
        }
}

def g = [ 1, 2 ]
def h = [ 2, -2 ]
println(h + g)
-------------------- gvector.groovy -----------------------

i expected that the output will be, [ 3, 0 ], but i got:

josanabr@linux:~/Documents/Groovy/testing/closure> groovy gvector
[2, -2, 1, 2]

the operator behaviour is the concatenation :-S.

Thanks for your help.


                    (`'`'`'`'`)
                     |       |
                     |       |
                     |       |
    -----..        (()----   |
   |        ||     (_        |
   |        ||       |       |
   |        ||       |       |
   |        ||       /\   ..--
   '--------''   /\  ||-''    \
      /   \      \ \//   ,,   \---.
   .---------.    \./ |~| /__\  \  |
___|_________|__|""-.___ / ||   |  |
|               | .-----'  ||   |  |
|               |CC.-----.      |  |
|               |  '-----'      |  |-ABG
                                |  |
Reply | Threaded
Open this post in threaded view
|

Re: [groovy-user] operator overloading

tugwilson

On 2 Nov 2005, at 19:08, [hidden email] wrote:

> Thanks for your help, i have another question
>
> i read in, http://groovy.codehaus.org/Operator+Overloading, that i can
> overload the operator, e.g. +, implementing the method plus inside  
> a class
> that i want to overload the operator. Sample from tutorial (Closure).
>
> Assume the next .groovy file:
>
> -------------------- gvector.groovy -----------------------
> public class GVector extends Vector {
>         public GVector() {
>                 super();
>         }
>
>         public void apply(c) {
>                 for (i in 0..<size()) {
>                         this[i] = c(this[i]);
>                 }
>         }
>         public GVector plus(b) {
>                 GVector result
>                 for (i in 0..<size())
>                   result[i] = this[i] + b[i]
>                 return result
>         }
> }
>
> def g = [ 1, 2 ]
> def h = [ 2, -2 ]
> println(h + g)
> -------------------- gvector.groovy -----------------------
>
> i expected that the output will be, [ 3, 0 ], but i got:
>
> josanabr@linux:~/Documents/Groovy/testing/closure> groovy gvector
> [2, -2, 1, 2]
>
> the operator behaviour is the concatenation :-S.
>
> Thanks for your help.

I think what's happening is that the plus method on Collection from  
DefaultGroovyMethods is overriding your plus method.

If this is so then it's a bug. I'm currently carrying out open heat  
surgery on the MetaClass which is responsible for this behaviour. I  
*think* I have fixed this bug. However it will be a few weeks before  
this fix makes it into CVS:(

Sorry about that.

John Wilson
The Wilson Partnership
http://www.wilson.co.uk


Reply | Threaded
Open this post in threaded view
|

Re: [groovy-user] operator overloading

Martin C. Martin
In reply to this post by John Sanabria


[hidden email] wrote:

>Thanks for your help, i have another question
>
>i read in, http://groovy.codehaus.org/Operator+Overloading, that i can
>overload the operator, e.g. +, implementing the method plus inside a class
>that i want to overload the operator. Sample from tutorial (Closure).
>
>Assume the next .groovy file:
>
>-------------------- gvector.groovy -----------------------
>public class GVector extends Vector {
>        public GVector() {
>                super();
>        }
>
>        public void apply(c) {
>                for (i in 0..<size()) {
>                        this[i] = c(this[i]);
>                }
>        }
>        public GVector plus(b) {
>                GVector result
>                for (i in 0..<size())
>                  result[i] = this[i] + b[i]
>                return result
>        }
>}
>
>def g = [ 1, 2 ]
>def h = [ 2, -2 ]
>println(h + g)
>-------------------- gvector.groovy -----------------------
>
>i expected that the output will be, [ 3, 0 ], but i got:
>
>josanabr@linux:~/Documents/Groovy/testing/closure> groovy gvector
>[2, -2, 1, 2]
>
>the operator behaviour is the concatenation :-S.
>  
>

You define the GVector class but never use it.  g and h are both
ArrayLists, and the plus() for ArrayLists is concatenation.

Reply | Threaded
Open this post in threaded view
|

Re: [groovy-user] operator overloading

tugwilson

On 2 Nov 2005, at 19:45, Martin C. Martin wrote:

>
> You define the GVector class but never use it.  g and h are both  
> ArrayLists, and the plus() for ArrayLists is concatenation.


Doh!

Thank you Martin!

John, pleas disregard my reply - it's utter nonsense :)



John Wilson
The Wilson Partnership
http://www.wilson.co.uk


Reply | Threaded
Open this post in threaded view
|

Re: [groovy-user] operator overloading

Jochen Theodorou
John Wilson schrieb:

>
> On 2 Nov 2005, at 19:45, Martin C. Martin wrote:
>
>>
>> You define the GVector class but never use it.  g and h are both  
>> ArrayLists, and the plus() for ArrayLists is concatenation.
>
> Thank you Martin!
>
> John, pleas disregard my reply - it's utter nonsense :)

not completely.

public GVector plus(b)

creates a method taking Object as parameter. But plus(Object) is not
enough, since there is a more special method in defaultGroovyMethods. It
has to be plus(GVector) to be sure it used then.

public GVector plus(GVector b)

bye blackdrag
Reply | Threaded
Open this post in threaded view
|

Re: [groovy-user] operator overloading

John Sanabria

> John Wilson schrieb:
>
>>
>> On 2 Nov 2005, at 19:45, Martin C. Martin wrote:
>>
>>>
>>> You define the GVector class but never use it.  g and h are both
>>> ArrayLists, and the plus() for ArrayLists is concatenation.
>>
>> Thank you Martin!
>>
>> John, pleas disregard my reply - it's utter nonsense :)
>
> not completely.
>
> public GVector plus(b)
>
> creates a method taking Object as parameter. But plus(Object) is not
> enough, since there is a more special method in defaultGroovyMethods. It
> has to be plus(GVector) to be sure it used then.
>
> public GVector plus(GVector b)
>
> bye blackdrag
>

ok, thanks so much for your reply, but:

1- I try to define g and h such as GVector and the behaviour doesn't
chance, the concatetanion follow
2- In the prototype of plus, i put GVector that follows:

        ...
        public GVector plus(GVector b) {
                GVector result
                for (i in 0..<size())
                  result[i] = this[i] + b[i]
                return result
        }
        ...

def g = new GVector()
g[0] = 1
g[1] = 2
def h = new GVector()
h[0] = 2
h[1] = 2
println( h + g )

and this is the output:

josanabr@linux:~/Documents/Groovy/testing/closure> !gr
groovy gvector
Caught: java.lang.NullPointerException: Cannot invoke method putAt() on
null object

thanks for your comments ;-)
Reply | Threaded
Open this post in threaded view
|

Re: [groovy-user] operator overloading

Russel Winder
On Wed, 2005-11-02 at 21:41 -0400, [hidden email] wrote:

>         public GVector plus(GVector b) {
>                 GVector result

 GVector result = new GVector ( )

>                 for (i in 0..<size())
>                   result[i] = this[i] + b[i]
>                 return result
>         }
>         ...

> Caught: java.lang.NullPointerException: Cannot invoke method putAt() on
> null object

result[i] is failing because it is null.

I notice that there is a javax.vecmath.GVector we have naming
competition!

BTW Where is the Groovy mail list archive?  I wanted to find the
amendments people were proposing to the GVector class but haven#t yet
made it to the Web site.

Thanks.
 
--
Russel.
====================================================
Dr Russel Winder                +44 20 7585 2200
41 Buckmaster Road              +44 7770 465 077
London SW11 1EN, UK             [hidden email]

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [groovy-user] operator overloading PS

Russel Winder
In reply to this post by John Sanabria
On Wed, 2005-11-02 at 21:41 -0400, [hidden email] wrote:

> def g = new GVector()
> g[0] = 1
> g[1] = 2

What infrastructure is needed to be able to say

def g = [ 1 , 2 ] as GVector

shouldn't it be just a question of providing the copy constructor

GVector ( ArrayList a )

Of course this doesn't work as there is a class cast exception if you
try it.
--
Russel.
====================================================
Dr Russel Winder                +44 20 7585 2200
41 Buckmaster Road              +44 7770 465 077
London SW11 1EN, UK             [hidden email]

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [groovy-user] operator overloading

Guillaume Laforge
Administrator
In reply to this post by Russel Winder
On 03/11/05, Russel Winder <[hidden email]> wrote:
> [...]
> BTW Where is the Groovy mail list archive?  I wanted to find the
> amendments people were proposing to the GVector class but haven#t yet
> made it to the Web site.

Just follow the mailing list link on the front page of our website :-)
The archive is hosted on gmane.

Codehaus' server seems a bit slow... or perhaps even crashed at the moment.

--
Guillaume Laforge
http://glaforge.free.fr/blog/groovy
Reply | Threaded
Open this post in threaded view
|

Re: [groovy-user] operator overloading PS

Guillaume Laforge
Administrator
In reply to this post by Russel Winder
On 03/11/05, Russel Winder <[hidden email]> wrote:

> On Wed, 2005-11-02 at 21:41 -0400, [hidden email] wrote:
>
> > def g = new GVector()
> > g[0] = 1
> > g[1] = 2
>
> What infrastructure is needed to be able to say
>
> def g = [ 1 , 2 ] as GVector
>
> shouldn't it be just a question of providing the copy constructor
>
> GVector ( ArrayList a )
>
> Of course this doesn't work as there is a class cast exception if you
> try it.

For this line to work:
def g = [ 1 , 2 ] as GVector

GVector needs to have a constructor taking two integer parameters.

--
Guillaume Laforge
http://glaforge.free.fr/blog/groovy
123