Sunday, February 24, 2013

Scala Inferring Nothing

A lot of times i've run an issue with scala inference and type parameters:
abstract class Activity[T] {
  def apply (): T

object Count extends Activity[Int] {
  def apply (): Int = ...
// a method that needs to know both A and T
def run [T, A <: Activity[T]] (activity: A) = ...

Trying to use the method:
error: inferred type arguments [Nothing,Count.type] do not conform to method run's type parameter bounds [T,A <: Activity[T]]

But it works if you pass the type parameters explicitly:
A solution:
def run [T, A <: Activity[T]] (activity: A with Activity[T]) = ...

The rule seems to be that the compiler will usually infer only the parameters that appear directly in the parameter list.