Type inferred to Nothing in Scala

Viewed 2096

When I try to compile the small example:

trait Foo[A,B] {
  type F[_,_]
  def foo(): F[A,B]
}

class Bar[A,B] extends Foo[A,B] {
  type F[D,E] = Bar[D,E]
  def foo() = this
}

object Helper {
  def callFoo[A,B,FF <: Foo[A,B]]( f: FF ): FF#F[A,B] =
    f.foo()
}

object Run extends App {
  val x = new Bar[Int,Double]
  val y = Helper.callFoo(x)
  println( y.getClass )
}

I get the error:

[error] src/Issue.scala:20: inferred type arguments
[Nothing,Nothing,issue.Bar[Int,Double]] do not conform to method callFoo's type
parameter bounds [A,B,FF <: issue.Foo[A,B]]
[error]       val y = Helper.callFoo(x)

Apparently, the type inference mechanism is not able to infer A and B out of Bar[A,B]. However, it works if I pass all the types by hand:

val y = Helper.callFoo[Int,Double,Bar[Int,Double]](x)

I there a way to avoid passing types explicitly?

2 Answers
Related