Re: Confusing AnyRef equals
Georgii Dernovyi <[email protected]> Wed, 14 Dec 2016 09:29:43 -0800 (PST)
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <[email protected]> |
------=_Part_4848_929823826.1481736583273
Content-Type: multipart/alternative;
boundary="----=_Part_4849_2079277116.1481736583274"
------=_Part_4849_2079277116.1481736583274
Content-Type: text/plain; charset=UTF-8
Thanks! )
On Thursday, December 15, 2016 at 12:16:58 AM UTC+7, Oliver Ruebenacker
wrote:
>
>
> 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]
> <javascript:>> 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]
>> <javascript:>> 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] <javascript:>.
>>> 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.
------=_Part_4849_2079277116.1481736583274
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Thanks! )<br><br>On Thursday, December 15, 2016 at 12:16:5=
8 AM UTC+7, Oliver Ruebenacker wrote:<blockquote class=3D"gmail_quote" styl=
e=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left:=
1ex;"><div dir=3D"ltr"><div><div><div><br></div>=C2=A0=C2=A0=C2=A0=C2=A0 H=
ello,<br><br></div>=C2=A0 What you seem to want can be written like this:<b=
r><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></s=
pan><br></div><div>=C2=A0=C2=A0=C2=A0=C2=A0 Best, Oliver<br></div><div><div=
><br></div></div></div><div><br><div class=3D"gmail_quote">On Wed, Dec 14, =
2016 at 12:07 PM, Oliver Ruebenacker <span dir=3D"ltr"><<a href=3D"javas=
cript:" target=3D"_blank" gdf-obfuscated-mailto=3D"t2VdjLJGCAAJ" rel=3D"nof=
ollow" onmousedown=3D"this.href=3D'javascript:';return true;" oncli=
ck=3D"this.href=3D'javascript:';return true;">[email protected]</a>&=
gt;</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><=
div><div><br></div>=C2=A0=C2=A0=C2=A0=C2=A0 Hello,<br><br></div>=C2=A0 It l=
ooks 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 th=
e 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 (=
Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).<br>Type in expressions f=
or evaluation. Or try :help.<br><br>scala> :paste<br>// Entering paste m=
ode (ctrl-D to finish)<span><br><br>case class a(i:Int)<br>{<br>=C2=A0 over=
ride def canEqual(a: Any) =3D a.isInstanceOf[Int]<br><br>=C2=A0 override de=
f 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 interpret=
ing.<br><br>defined class a<br><br>scala> val aa =3D new a(1)<br>aa: a =
=3D a(1)<br><br>scala> aa =3D=3D aa<br>res0: Boolean =3D false<br><br>sc=
ala> aa =3D=3D AnyRef<br>res1: Boolean =3D true<br><br>scala> AnyRef<=
br>res2: Specializable =3D object AnyRef<br><br></span></b><br></div>=C2=A0=
=C2=A0=C2=A0=C2=A0 Best, Oliver<br></div><div><div><div><br><div class=3D"g=
mail_quote">On Wed, Dec 14, 2016 at 11:00 AM, Georgii Dernovyi <span dir=3D=
"ltr"><<a href=3D"javascript:" target=3D"_blank" gdf-obfuscated-mailto=
=3D"t2VdjLJGCAAJ" rel=3D"nofollow" onmousedown=3D"this.href=3D'javascri=
pt:';return true;" onclick=3D"this.href=3D'javascript:';return =
true;">[email protected]</a>></span> wrote:<br><blockquote class=3D"gma=
il_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-lef=
t:1ex"><div dir=3D"ltr">I found that subj returns false for the same object=
. It is a terrible mistake for me<div><br><div><pre style=3D"color:rgb(0,0,=
0);font-family:"Courier New""><span style=3D"color:#000080;font-w=
eight:bold">case class </span>a(i:Int)<br>{<br> <span style=3D"color:#0000=
80;font-weight:bold">override def </span>canEqual(a: Any) =3D a.isInstanceO=
f[Int]<br><br> <span style=3D"color:#000080;font-weight:bold">override def=
</span>equals(o:Any) =3D <span style=3D"color:#660e7a;font-style:italic">A=
nyRef</span>.equals(o)<br><br> <span style=3D"color:#000080;font-weight:bo=
ld">override def </span>hashCode =3D <span style=3D"color:#660e7a;font-styl=
e:italic">AnyRef</span>.hashCode<br>}<br><br><span style=3D"color:#000080;f=
ont-weight:bold">val </span>aa =3D <span style=3D"color:#000080;font-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></pre><pre s=
tyle=3D"color:rgb(0,0,0);font-family:"Courier New""><span style=
=3D"color:#808080;font-style:italic"><br></span></pre><pre style=3D"color:r=
gb(0,0,0);font-family:"Courier New""><span style=3D"color:#808080=
;font-style:italic">Am I right? )</span></pre></div></div></div><span><font=
color=3D"#888888">
<p></p>
-- <br>
You received this message because you are subscribed to the Google Groups &=
quot;scala-language" group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"javascript:" target=3D"_blank" gdf-obfuscated-mailto=3D"=
t2VdjLJGCAAJ" rel=3D"nofollow" onmousedown=3D"this.href=3D'javascript:&=
#39;;return true;" onclick=3D"this.href=3D'javascript:';return true=
;">scala-languag...@<wbr>googlegroups.com</a>.<br>
For more options, visit <a href=3D"https://groups.google.com/d/optout" targ=
et=3D"_blank" rel=3D"nofollow" onmousedown=3D"this.href=3D'https://grou=
ps.google.com/d/optout';return true;" onclick=3D"this.href=3D'https=
://groups.google.com/d/optout';return true;">https://groups.google.com/=
d/<wbr>optout</a>.<br>
</font></span></blockquote></div><br><br clear=3D"all"><br></div></div><spa=
n><font color=3D"#888888">-- <br><div><div dir=3D"ltr"><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.type2diabetesgenet=
ics.org/" target=3D"_blank" rel=3D"nofollow" onmousedown=3D"this.href=3D=
9;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.type2diabetesgenetics.org=
%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFVE8xTGqlzANyuan2sVDo75uy-vA=
9;;return true;" onclick=3D"this.href=3D'http://www.google.com/url?q\x3=
dhttp%3A%2F%2Fwww.type2diabetesgenetics.org%2F\x26sa\x3dD\x26sntz\x3d1\x26u=
sg\x3dAFQjCNFVE8xTGqlzANyuan2sVDo75uy-vA';return true;">Diabetes Portal=
</a>, <a href=3D"http://www.broadinstitute.org/" target=3D"_blank" rel=3D"n=
ofollow" onmousedown=3D"this.href=3D'http://www.google.com/url?q\x3dhtt=
p%3A%2F%2Fwww.broadinstitute.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjC=
NHloBLjHUP5Yhb6LyVOZ-JE5b4Y5A';return true;" onclick=3D"this.href=3D=
9;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.broadinstitute.org%2F\x26=
sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHloBLjHUP5Yhb6LyVOZ-JE5b4Y5A';retu=
rn true;">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><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" rel=3D"nofollow" onmousedown=
=3D"this.href=3D'http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.type2d=
iabetesgenetics.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFVE8xTGqlzAN=
yuan2sVDo75uy-vA';return true;" onclick=3D"this.href=3D'http://www.=
google.com/url?q\x3dhttp%3A%2F%2Fwww.type2diabetesgenetics.org%2F\x26sa\x3d=
D\x26sntz\x3d1\x26usg\x3dAFQjCNFVE8xTGqlzANyuan2sVDo75uy-vA';return tru=
e;">Diabetes Portal</a>, <a href=3D"http://www.broadinstitute.org/" target=
=3D"_blank" rel=3D"nofollow" onmousedown=3D"this.href=3D'http://www.goo=
gle.com/url?q\x3dhttp%3A%2F%2Fwww.broadinstitute.org%2F\x26sa\x3dD\x26sntz\=
x3d1\x26usg\x3dAFQjCNHloBLjHUP5Yhb6LyVOZ-JE5b4Y5A';return true;" onclic=
k=3D"this.href=3D'http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.broad=
institute.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHloBLjHUP5Yhb6LyVO=
Z-JE5b4Y5A';return true;">Broad Institute</a><br></div><br></div></div>=
</div></div></div></div></div></div>
</div>
</blockquote></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;scala-language" 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 />
------=_Part_4849_2079277116.1481736583274--
------=_Part_4848_929823826.1481736583273--