Confusing AnyRef equals

43 views
Skip to first unread message

Georgii Dernovyi

unread,
Dec 14, 2016, 11:08:39 AM12/14/16
to scala-language
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? )

Vlad Patryshev

unread,
Dec 14, 2016, 11:11:14 AM12/14/16
to scala-l...@googlegroups.com
This is beautiful.

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

Thanks,
-Vlad

On Wed, Dec 14, 2016 at 8:05 AM, Georgii Dernovyi <g.der...@gmail.com> wrote:

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Oliver Ruebenacker

unread,
Dec 14, 2016, 12:07:56 PM12/14/16
to scala-l...@googlegroups.com

     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 a

scala> val aa = new a(1)
aa: a = a(1)

scala> aa == aa
res0: Boolean = false

scala> aa == AnyRef
res1: Boolean = true

scala> AnyRef
res2: Specializable = object AnyRef


     Best, Oliver

On Wed, Dec 14, 2016 at 11:00 AM, Georgii Dernovyi <g.der...@gmail.com> 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 scala-language+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute

Oliver Ruebenacker

unread,
Dec 14, 2016, 12:16:58 PM12/14/16
to scala-l...@googlegroups.com

     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

Georgii Dernovyi

unread,
Dec 14, 2016, 12:29:43 PM12/14/16
to scala-language
Thanks! )
To unsubscribe from this group and stop receiving emails from it, send an email to scala-languag...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute

Reply all
Reply to author
Forward
0 new messages