The implicitly method
Scala's standard library provides a utility method to create concrete instances of types' availability implicitly. The method's name is also implicitly. Let's take a look at the function signature:
def implicitly[T](implicit e: T) = e
This implicitly method simply expects a type parameter, finds the implicit value available in scope, and summons and returns it to us. This is a good option available to us to tell whether a particular type's value is available in implicit scope. Let's look at an application of this method:
import java.time.{LocalDateTime}
object ImplicitParameter extends App {
implicit val dateNow = LocalDateTime.now()
def showDateTime(implicit date: LocalDateTime) = println(date)
val ldt = implicitly[LocalDateTime]
println(s"ldt value from implicit scope: $ldt")
} The following is the result:
ldt value from implicit scope: 2017-12-17T10:47:13.846
In the preceding code snippet, a call to implicitly, along with the type, returned us...