I got stuck for like 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)
}
// compiles
val foo = new Foo()
foo.foo(4)
//does not compile
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 figure out why that is. The only thing that I can think of is that scalac doesn't find implicits within a Type that doesn't have a Value Type accessible on any prefix. It cannot be referenced. Scalac apparently needs to access that Foo.this.foo
to resolve implicits in it, which it can't in this case.
I feel like that if you combine type classes and path dependent types, you are effectively doomed. You'll end up dealing with this kind of stuff. I did that because scalac wouldn't otherwise infer types in my API methods and user would have to declare them explicitly. So I chose this kind of design so that types are constructed in Foo[T]
and api methods use the existing type, but I hit several really ugly problems and bugs of this kind that made my app look like an overengineered crap...