Discussion:
[scala-language] Confusing AnyRef equals
Georgii Dernovyi
2016-12-14 16:00:38 UTC
Permalink
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 scala-language+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Georgii Dernovyi
2016-12-14 16:05:47 UTC
Permalink
On Wednesday, December 14, 2016 at 11:00:38 PM UTC+7, Georgii Dernovyi
Post by Georgii Dernovyi
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[a]
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 scala-language+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Vlad Patryshev
2016-12-14 16:10:51 UTC
Permalink
This is beautiful.

val AnyRef = new Specializable {
override def toString = "object AnyRef"
}


Thanks,
-Vlad
Post by Georgii Dernovyi
On Wednesday, December 14, 2016 at 11:00:38 PM UTC+7, Georgii Dernovyi
Post by Georgii Dernovyi
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[a]
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
For more options, visit https://groups.google.com/d/optout.
--
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 scala-language+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Georgii Dernovyi
2016-12-14 16:08:39 UTC
Permalink
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[a]

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 scala-language+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Oliver Ruebenacker
2016-12-14 17:07:53 UTC
Permalink
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
Post by Georgii Dernovyi
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
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 scala-language+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Oliver Ruebenacker
2016-12-14 17:16:53 UTC
Permalink
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
Post by Oliver Ruebenacker
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
Post by Georgii Dernovyi
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
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 scala-language+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Georgii Dernovyi
2016-12-14 17:29:43 UTC
Permalink
Thanks! )

On Thursday, December 15, 2016 at 12:16:58 AM UTC+7, Oliver Ruebenacker
Post by Oliver Ruebenacker
Hello,
*case class a(i:Int){ override def equals(o:Any) =
super[Object].equals(o) override def hashCode = super[Object].hashCode}*
Best, Oliver
Post by Oliver Ruebenacker
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
Post by Georgii Dernovyi
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
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 scala-language+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...