The implicit scope of a type T
is where the compiler will look when attempting to locate implicit instances for that type. It is composed of all the companion objects of types associated with T
.
To take a concrete example:
class Foo
object Foo {
implicit val bar: List[Foo] = List.empty
}
bar
is of type List[Foo]
, and is located within the companion object of Foo
, a type associated with List[Foo]
: it’s in the implicit scope of List[Foo]
and we need no special import for the compiler to locate it.
implicitly[List[Foo]]
// res0: List[Foo] = List()
The implicit scope is particularly helpful when defining type class instances.