« Return to Thread: Overloading comparison operators

Re: Overloading comparison operators

by Jörg Staudemeyer-2 :: Rate this Message:

Reply to Author | View in Thread

I'm experimenting with a kind of query language. In this language, arithmetic and logical expression shall result in a description of the expression, not in the result of the actual expression evaluation itself. Hence the operator method should be able to return something at will and not be restricted to a certain result type.

Just as an example, think of the following.

(A op B) results in: new Operation(A,B,'op')

With arithmetic operators, this works perfectly using an operator method like this:

def plus(otherValue)  { new Operation(this,otherValue,'plus') }

or in a generic way:

def invokeMethod(String name, Object args) {
    if (args.size()==1) { return new Operation(this,args[0]),name) }
    ...
}

With Groovy's comparison operators there is currently no chance to do something like that.

-Jörg
 

2008/7/17 Guillaume Laforge <glaforge@...>:
By the way, I'd be curious to know what you have in mind?
What kind of DSL you would write with these operators?

On Thu, Jul 17, 2008 at 11:03 AM, Jörg Staudemeyer
<jstaudemeyer@...> wrote:
> 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
>
>



--
Guillaume Laforge
Groovy Project Manager
G2One, Inc. Vice-President Technology
http://www.g2one.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email





--
A Groovy user

 « Return to Thread: Overloading comparison operators