Confusing AnyRef equals

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Confusing AnyRef equals

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 email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Confusing AnyRef equals

Georgii Dernovyi


On Wednesday, December 14, 2016 at 11:00:38 PM UTC+7, Georgii Dernovyi 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[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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Confusing AnyRef equals

Vlad Patryshev
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 <[hidden email]> wrote:


On Wednesday, December 14, 2016 at 11:00:38 PM UTC+7, Georgii Dernovyi 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[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 [hidden email].
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Confusing AnyRef equals

Oliver Ruebenacker
In reply to this post by Georgii Dernovyi

     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 <[hidden email]> 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 [hidden email].
For more options, visit https://groups.google.com/d/optout.



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

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Confusing AnyRef equals

Oliver Ruebenacker

     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 <[hidden email]> 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 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 <[hidden email]> 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 [hidden email].
For more options, visit https://groups.google.com/d/optout.



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




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

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Confusing AnyRef equals

Georgii Dernovyi
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 <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="t2VdjLJGCAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">cur...@...> 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 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 <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="t2VdjLJGCAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">g.der...@...> 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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="t2VdjLJGCAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">scala-languag...@googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Senior Software Engineer, <a href="http://www.type2diabetesgenetics.org/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.type2diabetesgenetics.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFVE8xTGqlzANyuan2sVDo75uy-vA&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.type2diabetesgenetics.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFVE8xTGqlzANyuan2sVDo75uy-vA&#39;;return true;">Diabetes Portal, <a href="http://www.broadinstitute.org/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.broadinstitute.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHloBLjHUP5Yhb6LyVOZ-JE5b4Y5A&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.broadinstitute.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHloBLjHUP5Yhb6LyVOZ-JE5b4Y5A&#39;;return true;">Broad Institute




--
Oliver Ruebenacker
Senior Software Engineer, <a href="http://www.type2diabetesgenetics.org/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.type2diabetesgenetics.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFVE8xTGqlzANyuan2sVDo75uy-vA&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.type2diabetesgenetics.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFVE8xTGqlzANyuan2sVDo75uy-vA&#39;;return true;">Diabetes Portal, <a href="http://www.broadinstitute.org/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.broadinstitute.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHloBLjHUP5Yhb6LyVOZ-JE5b4Y5A&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.broadinstitute.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHloBLjHUP5Yhb6LyVOZ-JE5b4Y5A&#39;;return true;">Broad Institute

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.