2

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

Heptic
  • 3,076
  • 4
  • 30
  • 51
  • 2
    With ``data.sum[B]`` it works. Looks as if type inference is the problem, but why, I can't say. – Malte Schwerhoff Aug 14 '12 at 07:23
  • 1
    Not an answer to your question, but as a workaround you could move the implicit argument to the `Wrapper` constructor (it's intended to match the type `Vector[A]` after all). – Miles Sabin Aug 14 '12 at 07:25
  • The problems seems that data.sum is polymorphic as well... can you provide more details? – Edmondo Aug 14 '12 at 08:05

1 Answers1

2

§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.

incrop
  • 2,698
  • 1
  • 18
  • 17