Re: Confusing AnyRef equals

Oliver Ruebenacker <[email protected]> Wed, 14 Dec 2016 12:16:53 -0500
Newsgroups gmane.comp.lang.scala
Message-ID <CAA=X4ODb2MB3=4580ue+pp0qDYwNLf3d+FvZ9ZFbWkgFBPTSzw@mail.gmail.com>
--001a114a8868d9c47a0543a18059
Content-Type: text/plain; charset=UTF-8

     Hello,

  What you seem to want can be written like this:







*case class a(i:Int){  override def equals(o:Any) =
super[Object].equals(o)  override def hashCode = super[Object].hashCode}*
     Best, Oliver


On Wed, Dec 14, 2016 at 12:07 PM, Oliver Ruebenacker <[email protected]>
wrote:

>
>      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/>
>
>


-- 
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.

--001a114a8868d9c47a0543a18059
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 What you seem to want can be written like this:<br><br><=
span style=3D"font-family:monospace,monospace"><b>case class a(i:Int)<br>{<=
br>=C2=A0 override def equals(o:Any) =3D super[Object].equals(o)<br>=C2=A0 =
override def hashCode =3D super[Object].hashCode<br>}<br><br></b></span><br=
></div><div>=C2=A0=C2=A0=C2=A0=C2=A0 Best, Oliver<br></div><div><div><br></=
div></div></div><div class=3D"gmail_extra"><br><div class=3D"gmail_quote">O=
n Wed, Dec 14, 2016 at 12:07 PM, Oliver Ruebenacker <span dir=3D"ltr">&lt;<=
a href=3D"mailto:[email protected]" target=3D"_blank">[email protected]</a>&g=
t;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0=
 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div><d=
iv><div><br></div>=C2=A0=C2=A0=C2=A0=C2=A0 Hello,<br><br></div>=C2=A0 It lo=
oks 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.<br><br><b=
><span style=3D"font-family:monospace,monospace">Welcome to Scala 2.11.8 (J=
ava HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).<br>Type in expressions fo=
r evaluation. Or try :help.<br><br>scala&gt; :paste<br>// Entering paste mo=
de (ctrl-D to finish)<span class=3D""><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 override def =
hashCode =3D AnyRef.hashCode<br>}<br><br></span>// 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"=
><div><div class=3D"h5"><br><div class=3D"gmail_quote">On Wed, Dec 14, 2016=
 at 11:00 AM, Georgii Dernovyi <span dir=3D"ltr">&lt;<a href=3D"mailto:g.de=
[email protected]" target=3D"_blank">[email protected]</a>&gt;</span> wro=
te:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-=
left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr">I found that subj re=
turns false for the same object. It is a terrible mistake for me<div><br><d=
iv><pre style=3D"color:rgb(0,0,0);font-family:&quot;Courier New&quot;"><spa=
n style=3D"color:#000080;font-weight:bold">case class </span>a(i:Int)<br>{<=
br>  <span style=3D"color:#000080;font-weight:bold">override def </span>can=
Equal(a: Any) =3D a.isInstanceOf[Int]<br><br>  <span style=3D"color:#000080=
;font-weight:bold">override def </span>equals(o:Any) =3D <span style=3D"col=
or:#660e7a;font-style:italic">AnyRef</span>.equals(o)<br><br>  <span style=
=3D"color:#000080;font-weight:bold">override def </span>hashCode =3D <span =
style=3D"color:#660e7a;font-style:italic">AnyRef</span>.hashCode<br>}<br><b=
r><span style=3D"color:#000080;font-weight:bold">val </span>aa =3D <span st=
yle=3D"color:#000080;font-weight:bold">new </span>a(<span style=3D"color:#0=
000ff">1</span>)<br>aa =3D=3D aa <span style=3D"color:#808080;font-style:it=
alic">//false</span></pre><pre style=3D"color:rgb(0,0,0);font-family:&quot;=
Courier New&quot;"><span style=3D"color:#808080;font-style:italic"><br></sp=
an></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><span class=3D"m_6172690666317500780HOEnZb"><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@goo<wbr>glegroups.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/op<wbr>tout</a>.<br>
</font></span></blockquote></div><br><br clear=3D"all"><br></div></div><spa=
n class=3D"HOEnZb"><font color=3D"#888888">-- <br><div class=3D"m_617269066=
6317500780gmail_signature" data-smartmail=3D"gmail_signature"><div dir=3D"l=
tr"><div><div dir=3D"ltr"><div><div dir=3D"ltr"><div><div dir=3D"ltr"><div>=
Oliver 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><br></div><br></div></div></div></div></div></div></div></div>
</font></span></div>
</blockquote></div><br><br clear=3D"all"><br>-- <br><div class=3D"gmail_sig=
nature" 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>Oliver Ruebenacke=
r<br></div><div>Senior Software Engineer, <a href=3D"http://www.type2diabet=
esgenetics.org/" target=3D"_blank">Diabetes Portal</a>, <a href=3D"http://w=
ww.broadinstitute.org/" target=3D"_blank">Broad Institute</a><br></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 />

--001a114a8868d9c47a0543a18059--