Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem is that <code>Value</code> implemenents <code>Ordered[Enumeration#Value]</code>, rather than <code>Ordered[Dogs.Value]</code>. I don't know the rationale for this, it not be possible to do it the other way.</p> <p>This is sufficient for the simple case of directly comparing two values -- its just a normal method call:</p> <pre><code>scala&gt; (Rover: Ordered[Enumeration#Value]).&lt;(Sam) res44: Boolean = false </code></pre> <p>However, the type parameter <code>A</code> in <code>Ordered</code> is invariant, so when you you ask for type param to viewable as <code>Ordered[A]</code>, it is not sufficient to use <code>Dogs.Value &lt;: Ordered[Enumeration#Value]</code>. If <code>A</code> were contra-variant this would be allowed, but it also causes other problems with type inference.</p> <p>You could work around the problem by statically typing the list with <code>Enumeration#Value</code>:</p> <pre><code>scala&gt; val xs = List[Enumeration#Value](Rover, Tom, Sam, Sam, Rover) xs: List[Enumeration#Value] = List(Rover, Tom, Sam, Sam, Rover) scala&gt; doSomething(xs) List(Sam, Sam, Tom, Rover, Rover) </code></pre> <p>Or, by explicitly passing the type parameter to <code>doSomething</code>:</p> <pre><code>scala&gt; doSomething[Enumeration#Value](List(Rover, Sam)) List(Sam, Rover) </code></pre> <p>Or, better yet, loosening the requirements on the type parameters, essentially treating <code>Ordered</code> as contravariant in this circumstance.</p> <pre><code>scala&gt; def doSomething[A &lt;% Ordered[_ &gt;: A]](xs : List[A]) = xs sortWith (_ &lt; _) doSomething: [A](xs: List[A])(implicit evidence$1: (A) =&gt; Ordered[_ &gt;: A])List[A] scala&gt; doSomething(List(Rover, Sam)) res47: List[Dogs.Value] = List(Sam, Rover) </code></pre> <p>Why does that work?</p> <pre><code>scala&gt; Rover: Ordered[_ &lt;: Enumeration#Value] res52: scala.math.Ordered[_ &lt;: Enumeration#Value] = Rover </code></pre>
 

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