[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to overload the operator !=?

ankyhe

12/13/2006 8:49:00 AM

I overload ==, >, <, >=, <=successfully, however I can't overload != .
How to overload operator !=?

8 Answers

Vlad Galu

12/13/2006 8:54:00 AM

0

On 12/13/06, ankyhe@gmail.com <ankyhe@gmail.com> wrote:
> I overload ==, >, <, >=, <=successfully, however I can't overload != .
> How to overload operator !=?

Overloading "==" should do it :)

>
>
>


--
If it's there, and you can see it, it's real.
If it's not there, and you can see it, it's virtual.
If it's there, and you can't see it, it's transparent.
If it's not there, and you can't see it, you erased it.

Jano Svitok

12/13/2006 1:03:00 PM

0

On 12/13/06, Vlad Galu <dudu@dudu.ro> wrote:
> On 12/13/06, ankyhe@gmail.com <ankyhe@gmail.com> wrote:
> > I overload ==, >, <, >=, <=successfully, however I can't overload != .
> > How to overload operator !=?
>
> Overloading "==" should do it :)

!= is a defined using == i.e. a != b is parsed as !(a == b)
that means: 1. you cannot override !=, 2. you have to override ==

Vlad Galu

12/13/2006 2:20:00 PM

0

On 12/13/06, Jan Svitok <jan.svitok@gmail.com> wrote:
> On 12/13/06, Vlad Galu <dudu@dudu.ro> wrote:
> > On 12/13/06, ankyhe@gmail.com <ankyhe@gmail.com> wrote:
> > > I overload ==, >, <, >=, <=successfully, however I can't overload != .
> > > How to overload operator !=?
> >
> > Overloading "==" should do it :)
>
> != is a defined using == i.e. a != b is parsed as !(a == b)
> that means: 1. you cannot override !=, 2. you have to override ==

Which is precisely what I meant :)

>


--
If it's there, and you can see it, it's real.
If it's not there, and you can see it, it's virtual.
If it's there, and you can't see it, it's transparent.
If it's not there, and you can't see it, you erased it.

Jano Svitok

12/13/2006 3:48:00 PM

0

On 12/13/06, Vlad Galu <dudu@dudu.ro> wrote:
> On 12/13/06, Jan Svitok <jan.svitok@gmail.com> wrote:
> > On 12/13/06, Vlad Galu <dudu@dudu.ro> wrote:
> > > On 12/13/06, ankyhe@gmail.com <ankyhe@gmail.com> wrote:
> > > > I overload ==, >, <, >=, <=successfully, however I can't overload != .
> > > > How to overload operator !=?
> > >
> > > Overloading "==" should do it :)
> >
> > != is a defined using == i.e. a != b is parsed as !(a == b)
> > that means: 1. you cannot override !=, 2. you have to override ==
>
> Which is precisely what I meant :)

Right, I just though some clarification would not harm ;-) I should've
written that...

Tom Werner

12/13/2006 7:33:00 PM

0

ankyhe@gmail.com wrote:
> I overload ==, >, <, >=, <=successfully, however I can't overload != .
> How to overload operator !=?
>
>
>
>

The != token is not really a method at all and thus cannot be redefined.
It is, rather, syntactic sugar that calls == and negates the result. You
can see for yourself that this is true:

class Foo
def ==(other)
puts '== called'
true
end
end

f1 = Foo.new
f2 = Foo.new

f1 == f2
f1 != f2

Which outputs

== called
== called

And upon reflection, this makes sense, as in any sane world !(f1 == f2)
will equal (f1 != f2), and so redefining != would be redundant.

Tom

Robert Klemme

12/13/2006 9:52:00 PM

0

On 13.12.2006 20:33, Tom Werner wrote:
> And upon reflection, this makes sense, as in any sane world !(f1 == f2)
> will equal (f1 != f2), and so redefining != would be redundant.

It's been a long day and I'm not sure whether my logic fails me here,
but - from the above seems to follow that C++ is potentially insane.
Not that I didn't know that before - but it's a nice outcome of a thread
about operators. :-)

robert

Tom Werner

12/13/2006 10:18:00 PM

0

Robert Klemme wrote:
> On 13.12.2006 20:33, Tom Werner wrote:
>> And upon reflection, this makes sense, as in any sane world !(f1 ==
>> f2) will equal (f1 != f2), and so redefining != would be redundant.
>
> It's been a long day and I'm not sure whether my logic fails me here,
> but - from the above seems to follow that C++ is potentially insane.
> Not that I didn't know that before - but it's a nice outcome of a
> thread about operators. :-)
>

Half-way related amusing anecdote (from
http://rollerweblogger.org/roller/entry/arguing_with_sc...):

"Speaking of arguing with Scott Meyers... I worked at one of those phone
company research labs back in the 90's and we had enough money to bring
in Scott Meyers to teach us all about C++. Scott was explaining how you
can overload operators and you can even overload the equals sign, when
one student raised his hand. The student explained that there were some
situations in telecommunications systems where A was equal to B, but B
was not equal to A. Scott immediately objected, of course, but the
student went off into some jargon-filled explanation of his particular
problem domain. Scott let the student finish and then said, "if you were
to overload the equals operator so that A was equal to B, but B was not
equal to A, then /I would want to kill you/." That was the end of the
argument."

Tom

Sylvain Joyeux

12/14/2006 7:20:00 AM

0

On Wednesday 13 December 2006 22:55, Robert Klemme wrote:
> On 13.12.2006 20:33, Tom Werner wrote:
> > And upon reflection, this makes sense, as in any sane world !(f1 ==
> > f2) will equal (f1 != f2), and so redefining != would be redundant.
Well, I remember a thread on ruby-lang where somebody were trying to build
CSPs (constraint systems) using overloaded operators. In CSPs, you encode
== and != differently and since you can't overload both in Ruby, he could
not do what he wanted.

He could have in C++ (and somebody at my lab did ;-))
--
Sylvain Joyeux