Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>(See also two related questions: <a href="https://stackoverflow.com/questions/6881393/scala-fails-to-infer-the-right-type-arguments">Scala fails to infer the right type arguments</a> and <a href="https://stackoverflow.com/questions/6888136/type-infered-to-nothing-in-scala">Type infered to Nothing in Scala</a>)</p> <p>It looks like a limitation of Scala's type inference, which is intentionally not spec'ed. As work-around, you can get inference by making <code>T</code> a type <em>member</em> of <code>B</code> rather than parameter,</p> <pre><code>abstract class A abstract class B { type T &lt;: A } class ConcreteA extends A class ConcreteB extends B { type T = ConcreteA } class Example[U &lt;: B]( resolver: U ) object Test { new Example( new ConcreteB ) } </code></pre> <p>When using type members, it's useful to know that they can be surfaced as type parameters using refinement, as in Miles Sabin's answer to: <a href="https://stackoverflow.com/questions/6719827/why-is-this-cyclic-reference-with-a-type-projection-illegal">Why is this cyclic reference with a type projection illegal?</a></p> <p>In Jean-Philippe Pellet's answer to <a href="https://stackoverflow.com/questions/6888136/type-infered-to-nothing-in-scala">a related question</a>, type inference was aided by making the type parameter higher kinded. If you <em>introduce an extra type parameter</em> in <code>ConcreteB</code>, then type inference can work,</p> <pre><code>abstract class A abstract class B[T &lt;: A] class ConcreteA extends A class ConcreteB[T &lt;: A] extends B[T] class Example[T &lt;: A, U[T0 &lt;: A] &lt;: B[T0]]( resolver: U[T] ) object Test { new Example( new ConcreteB[ConcreteA] ) } </code></pre> <p>Scala 2.9 gives the mysterious error message below, but Miles Sabin points out it is a bug that will be fixed for 2.9.1</p> <pre><code>&lt;console&gt;:15: error: kinds of the type arguments (ConcreteA,ConcreteB[T0]) do not conform to the expected kinds of the type parameters (type T,type U) in class Example. ConcreteB[T0]'s type parameters do not match type U's expected parameters: class ConcreteB has one type parameter, but type U has one new Example( new ConcreteB[ConcreteA] ) ^ </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