In the minimal type class example below, scala 2.12.6 / sbt 1.2.1 complains could not find implicit value for parameter tc: tryout.Tryout.TypeClassTrait[Int]
. If I uncomment the println
line, it compiles. I was expecting this to work without the println because I import everything from the companion object.
Is this a bug or a feature? (By the way: Setting the scalac option -Xlog-implicits
did not produce anything helpful.)
trait Tryout {
import Tryout._
// println(IntTypeClass) // uncomment this line to make compile work
show(3) // compile fails here
}
object Tryout {
trait TypeClassTrait[T] {
def show(t: T): Unit
}
implicit object IntTypeClass extends TypeClassTrait[Int] {
override def show(t: Int): Unit = println(t)
}
def show[T](t: T)(implicit tc: TypeClassTrait[T]): Unit = tc.show(t)
}