title: Scala interop with Java questionmark generics
body: <p><strong>EDIT</strong> I have made the example self-contained.</p>
  
  <p>Suppose in java I have</p>
  
  <p>src/main/java/FooFactory.java</p>
  
  <pre><code>interface FooFactory {
    Foo&lt;?&gt; create();
    &lt;T&gt; void enhance(Foo&lt;T&gt; foo, FooEnhancer&lt;? super T&gt; enhancer);
  }
  </code></pre>
  
  <p>and src/main/java/Foo.java</p>
  
  <pre><code>interface Foo&lt;T&gt; {}
  </code></pre>
  
  <p>and src/main/java/FooEnhancer.java</p>
  
  <pre><code>interface FooEnhancer&lt;T&gt; {}
  </code></pre>
  
  <p>(and you can't change these interfaces because they belong to someone else.)</p>
  
  <p>Then in scala you have</p>
  
  <pre><code>object DummyFooEnhancer extends FooEnhancer[Any]
  
  
  trait FooHdlr {
    def fooFactory: FooFactory
  
    val foo = fooFactory.create
    fooFactory.enhance(foo, DummyFooEnhancer)
  
  }
  </code></pre>
  
  <p>This doesn't compile because FooEnhancer and Foo are invariant but <code>foo</code> is a <code>Foo&lt;?&gt;</code> whereas <code>DummyFooEnhancer</code> is a <code>Foo[Any]</code>. Changing <code>Any</code> to <code>AnyRef</code> doesn't work for the same reason.</p>
  
  <p>Then I reasoned that, well, ? is a <em>specific</em> type that is certainly not known at compile time and that may not necessarily be <code>AnyRef</code>/<code>Object</code>, so what if I do this?</p>
  
  <pre><code>case class DummyFooEnhancer[T] extends FooEnhancer[T]
  
  
  trait FooHdlr {
    def fooFactory: FooFactory
  
    val foo = fooFactory.create
    fooFactory.enhance(foo, new DummyFooEnhancer)
  
  }
  </code></pre>
  
  <p>But this causes scalac to stackoverflow! It's looping with </p>
  
  <pre><code>at scala.reflect.internal.Types$TypeVar.isGround(Types.scala:3082)
  </code></pre>
  
  <p>calling itself.</p>
  
  <p>Interestingly if I replace <code>? super T</code> with <code>T</code> it works fine, but in real life I can't do this because it's in code I don't control</p>