« Return to Thread: operator overloading

Re: operator overloading

by Warner Onstine-3 :: Rate this Message:

Reply to Author | View in Thread

On 10/1/07, Tom Nichols <tmnichols@...> wrote:
> Ah, yes I see your issue.  The > and < operators (comparison) assume that
> A < B  OR B <= A will always be true.  When you're talking about sets,
> A may not be a subset of B AND B might not be a subset of A.  So I
> suppose the less than/ greater than does not really equate to subset
> notation.  You could use leftShift and rightShift...

Unfortunately not quite the same (especially since you can have
subsets and proper subsets). Not to mention I think the notation is
unclear in the given context (which is what the DSL is all about
anyways).

There are of course other operators I could override, but the <=, <,
=>, > make the most sense to me given the context. At this point I've
put in a comment on http://jira.codehaus.org/browse/GROOVY-1889 in
hopes that we can get this in for Groovy 2.0. It hurts to be behind
Ruby in this way (heck, even C# allows you to override these methods
specifically).

Thanks for all the help, just sorry that I wasn't mistaken.

-warner

>
> Try this?
>
> class MySet {
>   def items = []
>   public boolean leftShift(other) {
>     if( ! (other instanceof MySet) ) return false
>     (this.items - other.items).size == 0
>   }
>
>   public boolean rightShift(other) {
>     if( ! (other instanceof MySet) ) return -1
>     (other.items - this.items).size == 0
>   }
>
>   public boolean equals( other ) {
>     if( ! (other instanceof MySet) ) return -1
>     (other.items - this.items).size == 0 &&
>       (this.items - other.items).size == 0
>   }
> }
>
> def set1 = new MySet( items:[1,2,3] )
> def set2 = new MySet( items:[1,2,3] )
> assert set1 == set2
> assert set1 << set2 && set2 << set1
> assert set1 >> set2 && set2 >> set1
>
> set2.items -= 3   //set2 == [1,2]
> assert set1 != set2
> assert set2 << set1 && set1 >> set2
> assert !(set1 << set2) && !(set2 >> set1)
>
> set3 = new MySet( items:[1,8] )
> assert !( set1 >> set3 ) && !( set3 >> set1 )
> assert !( set1 << set3 ) && !( set3 << set1 )
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>


--
Warner Onstine - Programmer/Author
New book on Tapestry 4!
Tapestry 101 available at
http://sourcebeat.com/books/tapestrylive.html
warner@...
http://warneronstine.com/blog

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

    http://xircles.codehaus.org/manage_email

 « Return to Thread: operator overloading