Note that there are some explanatory texts on larger screens.

plurals
  1. POCovariant Typeparameter in scala needs to be invariant in java interface
    primarykey
    data
    text
    <p>I've got a trait that looks like this (some further information can be found at <a href="https://stackoverflow.com/questions/3695990/inheritance-and-automatic-type-conversion">this related question by myself</a> although I don't think, it's needed for this question)</p> <pre><code>trait Extractor[-A,+B] { def extract(d:A):B //lots of other things } </code></pre> <p>To use this in an existing java framework I would like this <code>Extractor</code> to either have a function that returns a <code>Comparator[B]</code> (being <a href="http://download.oracle.com/javase/6/docs/api/java/util/Comparator.html" rel="nofollow noreferrer">java.util.Comparator</a>) or even better extend <code>Comparator[A]</code>. Now that poses a problem because <code>Comparator</code>s type parameter is ought to be invariant, while <code>A</code> is contravariant and <code>B</code> is covariant.</p> <p>So I get errors like this:</p> <pre><code>scala&gt; import java.util.Comparator import java.util.Comparator scala&gt; trait Extractor[-A,+B] extends Comparator[A] &lt;console&gt;:6: error: contravariant type A occurs in invariant position in type [-A,+B]java.lang.Object with java.util.Comparator[A] of trait Extractor trait Extractor[-A,+B] extends Comparator[A] ^ scala&gt; trait Extractor[-A, +B] { | def comp:Comparator[B] | } &lt;console&gt;:7: error: covariant type B occurs in invariant position in type =&gt; java.util.Comparator[B] of method comp def comp:Comparator[B] ^ </code></pre> <p>Do you see any way out of this or is this just one of those cases where "using java generics in scala hurts"?</p>
    singulars
    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.
 

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