Re: Confusing AnyRef equals

Oliver Ruebenacker <[email protected]> Wed, 14 Dec 2016 12:07:53 -0500
Newsgroups gmane.comp.lang.scala
Message-ID <CAA=X4OBTNkQrh+LKVf6ifEXD8UvV1TZCoLY7t0LFYxspiTSCWg@mail.gmail.com>
--001a114108faa918500543a1609d
Content-Type: text/plain; charset=UTF-8

     Hello,

  It looks like you may have intended to call the AnyRef-implementation of
equals of this object, but in fact AnyRef.equals(o) calls the equals method
of the AnyRef object, which is a different object, so it returns false.
































*Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java
1.8.0_45).Type in expressions for evaluation. Or try :help.scala> :paste//
Entering paste mode (ctrl-D to finish)case class a(i:Int){  override def
canEqual(a: Any) = a.isInstanceOf[Int]  override def equals(o:Any) =
AnyRef.equals(o)  override def hashCode = AnyRef.hashCode}// Exiting paste
mode, now interpreting.defined class ascala> val aa = new a(1)aa: a =
a(1)scala> aa == aares0: Boolean = falsescala> aa == AnyRefres1: Boolean =
truescala> AnyRefres2: Specializable = object AnyRef*
     Best, Oliver

On Wed, Dec 14, 2016 at 11:00 AM, Georgii Dernovyi <[email protected]>
wrote:

> I found that subj returns false for the same object. It is a terrible
> mistake for me
>
> case class a(i:Int)
> {
>   override def canEqual(a: Any) = a.isInstanceOf[Int]
>
>   override def equals(o:Any) = AnyRef.equals(o)
>
>   override def hashCode = AnyRef.hashCode
> }
>
> val aa = new a(1)
> aa == aa //false
>
>
> Am I right? )
>
> --
> You received this message because you are subscribed to the Google Groups
> "scala-language" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal
<http://www.type2diabetesgenetics.org/>, Broad Institute
<http://www.broadinstitute.org/>

-- 
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.

--001a114108faa918500543a1609d
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div><div><div><br></div>=C2=A0=C2=A0=C2=A0=C2=A0 Hello,<b=
r><br></div>=C2=A0 It looks like you may have intended to call the AnyRef-i=
mplementation of equals of this object, but in fact AnyRef.equals(o) calls =
the equals method of the AnyRef object, which is a different object, so it =
returns false.<br><br><b><span style=3D"font-family:monospace,monospace">We=
lcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).<b=
r>Type in expressions for evaluation. Or try :help.<br><br>scala&gt; :paste=
<br>// Entering paste mode (ctrl-D to finish)<br><br>case class a(i:Int)<br=
>{<br>=C2=A0 override def canEqual(a: Any) =3D a.isInstanceOf[Int]<br><br>=
=C2=A0 override def equals(o:Any) =3D AnyRef.equals(o)<br><br>=C2=A0 overri=
de def hashCode =3D AnyRef.hashCode<br>}<br><br>// Exiting paste mode, now =
interpreting.<br><br>defined class a<br><br>scala&gt; val aa =3D new a(1)<b=
r>aa: a =3D a(1)<br><br>scala&gt; aa =3D=3D aa<br>res0: Boolean =3D false<b=
r><br>scala&gt; aa =3D=3D AnyRef<br>res1: Boolean =3D true<br><br>scala&gt;=
 AnyRef<br>res2: Specializable =3D object AnyRef<br><br></span></b><br></di=
v>=C2=A0=C2=A0=C2=A0=C2=A0 Best, Oliver<br></div><div class=3D"gmail_extra"=
><br><div class=3D"gmail_quote">On Wed, Dec 14, 2016 at 11:00 AM, Georgii D=
ernovyi <span dir=3D"ltr">&lt;<a href=3D"mailto:[email protected]" targe=
t=3D"_blank">[email protected]</a>&gt;</span> wrote:<br><blockquote clas=
s=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;pad=
ding-left:1ex"><div dir=3D"ltr">I found that subj returns false for the sam=
e object. It is a terrible mistake for me<div><br><div><pre style=3D"color:=
rgb(0,0,0);font-family:&quot;Courier New&quot;"><span style=3D"color:#00008=
0;font-weight:bold">case class </span>a(i:Int)<br>{<br>  <span style=3D"col=
or:#000080;font-weight:bold">override def </span>canEqual(a: Any) =3D a.isI=
nstanceOf[Int]<br><br>  <span style=3D"color:#000080;font-weight:bold">over=
ride def </span>equals(o:Any) =3D <span style=3D"color:#660e7a;font-style:i=
talic">AnyRef</span>.equals(o)<br><br>  <span style=3D"color:#000080;font-w=
eight:bold">override def </span>hashCode =3D <span style=3D"color:#660e7a;f=
ont-style:italic">AnyRef</span>.hashCode<br>}<br><br><span style=3D"color:#=
000080;font-weight:bold">val </span>aa =3D <span style=3D"color:#000080;fon=
t-weight:bold">new </span>a(<span style=3D"color:#0000ff">1</span>)<br>aa =
=3D=3D aa <span style=3D"color:#808080;font-style:italic">//false</span></p=
re><pre style=3D"color:rgb(0,0,0);font-family:&quot;Courier New&quot;"><spa=
n style=3D"color:#808080;font-style:italic"><br></span></pre><pre style=3D"=
color:rgb(0,0,0);font-family:&quot;Courier New&quot;"><span style=3D"color:=
#808080;font-style:italic">Am I right? )</span></pre></div></div></div><spa=
n class=3D"HOEnZb"><font color=3D"#888888">

<p></p>

-- <br>
You received this message because you are subscribed to the Google Groups &=
quot;scala-language&quot; group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:[email protected]" targ=
et=3D"_blank">scala-language+unsubscribe@<wbr>googlegroups.com</a>.<br>
For more options, visit <a href=3D"https://groups.google.com/d/optout" targ=
et=3D"_blank">https://groups.google.com/d/<wbr>optout</a>.<br>
</font></span></blockquote></div><br><br clear=3D"all"><br>-- <br><div clas=
s=3D"gmail_signature" data-smartmail=3D"gmail_signature"><div dir=3D"ltr"><=
div><div dir=3D"ltr"><div><div dir=3D"ltr"><div><div dir=3D"ltr"><div>Olive=
r Ruebenacker<br></div><div>Senior Software Engineer, <a href=3D"http://www=
.type2diabetesgenetics.org/" target=3D"_blank">Diabetes Portal</a>, <a href=
=3D"http://www.broadinstitute.org/" target=3D"_blank">Broad Institute</a><b=
r></div><br></div></div></div></div></div></div></div></div>
</div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;scala-language&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:[email protected]">scal=
[email protected]</a>.<br />
For more options, visit <a href=3D"https://groups.google.com/d/optout">http=
s://groups.google.com/d/optout</a>.<br />

--001a114108faa918500543a1609d--