I was stuck for an hour to discover this fact:
class Foo {
trait TypeClass[X]
object TypeClass {
implicit val gimme = new TypeClass[Int]{}
}
def foo[X : TypeClass](p: X): Unit = println("yeah " + p)
}
val foo = new Foo()
foo.foo(4)
new Foo().foo(4)
could not find implicit value for evidence parameter of type _1.TypeClass[Int]
[error] new Foo().foo(4)
[error]
I can’t understand why this is so. The only thing I can think of is that scalac does not find implicits in a type that does not have a value type available on any prefix. It cannot be referenced. Obviously, Scalac needs to access this Foo.this.footo resolve the implications in it, which in this case it cannot be.
, , , . . , scalac API-, . , Foo[T], api , , ...