jastrachan | 26 May 18:34 2004
Picon

Re: compareTo with Integer

Comparisons should typically be symmetric...

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/ 
Object.html#equals(java.lang.Object)

that is if (a > b) == !(b > a)

currently the code is checking if either side is a number and if so,  
comparing to numbers. Strictly speaking, I'm not sure comparing numbers  
to arbitrary objects is a good idea (since Integer.compareTo()) is not  
gonna understand how to perform the comparison). So maybe its time for  
your own Comparator?

What Groovy tries to do is convert the left hand side to a number if  
the right hand side is one - to get around various type coercion  
issues. Do you think this is causing you an issue?

On 26 May 2004, at 16:02, Baudet Laurent wrote:
> Hi every body, thank's for this script language.
>
> I have some problem with the Operator overloading compareTo.
> My java class like to this.
>
> public class someClass implements Comparable
> {
>     public int compareTo(Object aO)
>     {
>         return 0;
>     }
> }
>
> I want to wrote in groovy file
>
> x = new someClass();
> if (x > 3){
> //do something;
> }
>
> I have this execution error    :-(
>
> groovy.lang.GroovyRuntimeException: Could not convert object:  
> com.bodet.test.someClass@... into a Number
> 	at org.codehaus.groovy.runtime.Invoker.asNumber(Invoker.java:600)
> 	at org.codehaus.groovy.runtime.Invoker.compareTo(Invoker.java:402)
> 	at  
> org.codehaus.groovy.runtime.InvokerHelper.compareGreaterThan(InvokerHel 
> per.java:303)
> 	at Script1.run(Script1.groovy:3)
> 	at com.bodet.test.exempleComparable.main(exempleComparable.java:36)
>
> Thank's to explain me a good solution.
>
> Laurent Baudet
> BODET S.A.
> Cholet France
> _______________________________________________
> groovy-user mailing list
> groovy-user@...
> http://lists.codehaus.org/mailman/listinfo/groovy-user
>
>

James
-------
http://radio.weblogs.com/0112098/

Gmane