Note that there are some explanatory texts on larger screens.

plurals
  1. POScala Case Class equals implementation
    primarykey
    data
    text
    <p>Given the following the classes:</p> <pre><code>trait ModelWithId { var id: Long = 0 } case class EntityAttribute ( val entityId: UUID, val attrName: String, val stringVal: Option[String], val boolVal: Option[Boolean], val longVal: Option[Long], val doubleVal: Option[Double], val byteVal: Option[Array[Byte]]) extends ModelWithId{ override def toString() : String = { "EntityAttribute(" + entityId.hashCode + "," + attrName.hashCode + "," + stringVal.map{_.hashCode}.getOrElse(None) + "," + stringVal.hashCode+ "," + boolVal.map{_.hashCode}.getOrElse(None) + "," + boolVal.hashCode+ "," + longVal.map{_.hashCode}.getOrElse(None) + "," + longVal.hashCode+ "," + doubleVal.map{_.hashCode}.getOrElse(None) + "," + doubleVal.hashCode+ "," + byteVal.map{_.hashCode}.getOrElse(None) + ")" } } </code></pre> <p>And the following comparison functions:</p> <pre><code>val newAtttributes : List[EntityAttribute] val withoutIds : List[EntityAttribute] println("without: " + withoutIds) println("new: " + newAtttributes) val differences = newAtttributes.diff(withoutIds) println("diff: " + differences) if(newAtttributes.size == 1 &amp;&amp; withoutIds.size == 1){ println("==: " + (newAtttributes.get(0) == withoutIds.get(0))) println("equals: " + (newAtttributes.get(0).equals(withoutIds.get(0)))) println("hequals: " + (newAtttributes.get(0).hashCode == withoutIds.get(0).hashCode)) } </code></pre> <p>I get the expected difference output 99 out 100 times. Very occasionally the diff function will return an empty list when it should a list of one.</p> <p>Example:</p> <pre><code>without: List(EntityAttribute(428861607,-1147340381,None,120224,None,120224,56,-356863126,None,120224,None)) new: List(EntityAttribute(428861607,-1147340381,None,120224,None,120224,23,424930523,None,120224,None)) diff: List() ==: false equals: false hequals: false </code></pre> <p>I can reliably reproduce this error usually after around 10-18 iterations. The two lists are coming from different sources, so they are being constructed differently. I am guessing that it has something to do with auto-boxing or a bad hashCode implementation but I have been bashing my head into a wall for 2 days without progress.</p> <p>I'm using scala 2.9.0-1.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload