I have seen a function named implicitly used in Scala examples. What is it, and how is it used?
scala> sealed trait Foo[T] { def apply(list : List[T]) : Unit }; object Foo {
| implicit def stringImpl = new Foo[String] {
| def apply(list : List[String]) = println("String")
| }
| implicit def intImpl = new Foo[Int] {
| def apply(list : List[Int]) = println("Int")
| }
| } ; def foo[A : Foo](x : List[A]) = implicitly[Foo[A]].apply(x)
defined trait Foo
defined module Foo
foo: [A](x: List[A])(implicit evidence$1: Foo[A])Unit
scala> foo(1)
<console>:8: error: type mismatch;
found : Int(1)
required: List[?]
foo(1)
^
scala> foo(List(1,2,3))
Int
scala> foo(List("a","b","c"))
String
scala> foo(List(1.0))
<console>:8: error: could not find implicit value for evidence parameter of type
Foo[Double]
foo(List(1.0))
^
Note that we have to write implicitly[Foo[A]].apply(x) since the compiler thinks that implicitly[Foo[A]](x) means that we call implicitly with parameters.
Also see How to investigate objects/types/etc. from Scala REPL? and Where does Scala look for implicits?
Here are a few reasons to use the delightfully simple method
implicitly.To understand/troubleshoot Implicit Views
An Implicit View can be triggered when the prefix of a selection (consider for example,
the.prefix.selection(args)does not contain a memberselectionthat is applicable toargs(even after trying to convertargswith Implicit Views). In this case, the compiler looks for implicit members, locally defined in the current or enclosing scopes, inherited, or imported, that are either Functions from the type of thatthe.prefixto a type withselectiondefined, or equivalent implicit methods.Implicit Views can also be triggered when an expression does not conform to the Expected Type, as below:
Here the compiler looks for this function:
Accessing an Implicit Parameter Introduced by a Context Bound
Implicit parameters are arguably a more important feature of Scala than Implicit Views. They support the type class pattern. The standard library uses this in a few places — see
scala.Orderingand how it is used inSeqLike#sorted. Implicit Parameters are also used to pass Array manifests, andCanBuildFrominstances.Scala 2.8 allows a shorthand syntax for implicit parameters, called Context Bounds. Briefly, a method with a type parameter
Athat requires an implicit parameter of typeM[A]:can be rewritten as:
But what’s the point of passing the implicit parameter but not naming it? How can this be useful when implementing the method
foo?Often, the implicit parameter need not be referred to directly, it will be tunneled through as an implicit argument to another method that is called. If it is needed, you can still retain the terse method signature with the Context Bound, and call
implicitlyto materialize the value:Passing a subset of implicit parameters explicitly
Suppose you are calling a method that pretty prints a person, using a type class based approach:
What if we want to change the way that the name is output? We can explicitly call
PersonShow, explicitly pass an alternativeShow[String], but we want the compiler to pass theShow[Int].