Heptic
Heptic

Reputation: 3106

Why is trivial implicit not found?

Why isn't the implicit not found, even in something as trivial as:

class Wrapper[+A](data: Vector[A]) {
  def sum[B >: A](implicit num: Numeric[B]) = data.sum
}

won't compile, without resorting to manually passing in num to data.sum

Upvotes: 2

Views: 111

Answers (1)

incrop
incrop

Reputation: 2738

§7.2 of Scala specification (page 107) states that implicit parameters are inferred after any type arguments are inferred. I believe this is the problem.

Typer infers most specific parameter for data.sum - A, and then looks for implicit Numeric[A] in scope. He can't substitute it with Numeric[B] because Numeric is invariant.

Upvotes: 2

Related Questions