0

I have a trait that requires a function with a ClassTag

trait Foo[T] {
    def bar(a: List[T])(implicit ev: ClassTag[T]): Unit
}

Now I have a class that needs to extend that trait, but it also has uses for its ClassTag for other methods, e.g.

class MyClass[T](implicit classTag: ClassTag[T]) extends Foo[T] {
    override def bar(a: List[T])(implicit ev: ClassTag[T]): Unit = { /* do stuff */ }
    def other(p: Map[String, T]) = /* other stuff where I need the ClassTag */
}

When this is compiled I get an error message along the lines of:

Error:scalac: ambiguous implicit values:
both value classTag in class MyClass of type scala.reflect.ClassTag[T]
and value ev of type scala.reflect.ClassTag[T]
match expected type scala.reflect.ClassTag[T]

Is there a way I can accommodate both the class level ClassTag and override the method from the parent trait?

kanielc
  • 1,268
  • 1
  • 12
  • 14

1 Answers1

1

You can simply give the same name to both of them.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • Wow, did not see that one coming. I don't even know why that makes sense, but thank you. – kanielc Nov 19 '14 at 13:11
  • Consider it this way: in your case, the compiler could insert `classTag` or `ev` where it needs the implicit, and it doesn't know which to pick. If they are named the same, it can't refer to the constructor argument (it's _shadowed_ by the method argument), and so it isn't considered visible for implicit resolution. – Alexey Romanov Nov 19 '14 at 13:51
  • That actually makes sense. It's like using the compiler against itself. – kanielc Nov 19 '14 at 14:02