Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, let's address the problem of finding the exact instance you want:</p> <pre><code>scala&gt; trait AbstractOp defined trait AbstractOp scala&gt; case class Pop() extends AbstractOp { | override def equals(other: Any) = other match { | case that: Pop =&gt; this eq that | case _ =&gt; false | } | } defined class Pop scala&gt; case class PushInt(val i: Int) extends AbstractOp { | override def equals(other: Any) = other match { | case that: PushInt =&gt; this eq that | case _ =&gt; false | } | } defined class PushInt scala&gt; val l = List(PushInt(1), PushInt(1), Pop(), Pop()) l: List[Product with AbstractOp] = List(PushInt(1), PushInt(1), Pop(), Pop()) scala&gt; val op = l(1) op: Product with AbstractOp = PushInt(1) scala&gt; println( l.indexOf( op ) ) 1 </code></pre> <p>That, of course, mean <code>PushInt(1) != PushInt(1)</code>, unless it is the exact same instance of <code>PushInt(1)</code>. It doesn't break <code>equals</code>/<code>hashCode</code> contract because <code>a.equals(b) =&gt; a.hashCode == b.hashCode</code>, but <code>a.hashCode == b.hashCode</code> doesn't imply anything. But if your only use is <em>finding</em> that instance, try this instead:</p> <pre><code>scala&gt; case class Pop() extends AbstractOp defined class Pop scala&gt; case class PushInt(val i: Int) extends AbstractOp defined class PushInt scala&gt; val l = List(PushInt(1), PushInt(1), Pop(), Pop()) l: List[Product with AbstractOp] = List(PushInt(1), PushInt(1), Pop(), Pop()) scala&gt; val op = l(1) op: Product with AbstractOp = PushInt(1) scala&gt; println( l.findIndexOf( op eq _ ) ) 1 </code></pre> <p>Either way, if you reinsert that instance in the list you'll have trouble. You have to make sure that each instance you insert is unique. You might even write your own collection, either throwing an exception if a repeated instance is inserted, or make a copy of any instance passed to it (easy enough with case classes and <code>copy</code> method on Scala 2.8).</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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