17 Jul 10:59
[groovy-user] Overloading comparison operators
From: Jörg Staudemeyer <jstaudemeyer@...>
Subject: [groovy-user] Overloading comparison operators
Newsgroups: gmane.comp.lang.groovy.user
Date: 2008-07-17 09:03:40 GMT
Subject: [groovy-user] Overloading comparison operators
Newsgroups: gmane.comp.lang.groovy.user
Date: 2008-07-17 09:03:40 GMT
Hi
are there any plans to change handling of comparison operators in a later version of Groovy?
Currently, comparison operators only apply to classes that implement the Comparable interface and are mapped to the compareTo() method. compareTo() method must return an integer value, as dictated by the interface, but the result of a comparison expression is always boolean.
IMO this is not consistent with handling of other operators like + and << that always map to a specific method returning an arbitrary result value. As a result, usage of comparison operators in DSLs is quite restricted.
What I would like to have is something like this:
a < b ==> a.smallerThan(b)
a =<b ==> a.smallerThanEqualOrEqualTo(b)
a!=b ==> a.notEqualTo(b)
and so on. All this methods should normally return a boolean value, but in certain circumstances, like in a DSL, can also return something else.
Implement this should not be particularly difficult, one could simply create default implementations of these methods for Comparable in DefaultGroovyMethods doing something constistent with current behaviour.
What do you think?
-Jörg
are there any plans to change handling of comparison operators in a later version of Groovy?
Currently, comparison operators only apply to classes that implement the Comparable interface and are mapped to the compareTo() method. compareTo() method must return an integer value, as dictated by the interface, but the result of a comparison expression is always boolean.
IMO this is not consistent with handling of other operators like + and << that always map to a specific method returning an arbitrary result value. As a result, usage of comparison operators in DSLs is quite restricted.
What I would like to have is something like this:
a < b ==> a.smallerThan(b)
a =<b ==> a.smallerThanEqualOrEqualTo(b)
a!=b ==> a.notEqualTo(b)
and so on. All this methods should normally return a boolean value, but in certain circumstances, like in a DSL, can also return something else.
Implement this should not be particularly difficult, one could simply create default implementations of these methods for Comparable in DefaultGroovyMethods doing something constistent with current behaviour.
What do you think?
-Jörg
RSS Feed